00001 #region --- License ---
00002
00003
00004
00005
00006
00007 #endregion
00008
00009 using System;
00010 using System.Collections.Generic;
00011 using System.Text;
00012
00013 namespace OpenTK.Graphics
00014 {
00020 public struct ColorFormat
00021 {
00022 byte red, green, blue, alpha;
00023 bool isIndexed;
00024 int bitsPerPixel;
00025
00026 #region --- Constructors ---
00027
00032 public ColorFormat(int bpp)
00033 {
00034 if (bpp < 0)
00035 throw new ArgumentOutOfRangeException("bpp", "Must be greater or equal to zero.");
00036 red = green = blue = alpha = 0;
00037 bitsPerPixel = bpp;
00038 isIndexed = false;
00039
00040 switch (bpp)
00041 {
00042 case 32:
00043 Red = Green = Blue = Alpha = 8;
00044 break;
00045 case 24:
00046 Red = Green = Blue = 8;
00047 break;
00048 case 16:
00049 Red = Blue = 5;
00050 Green = 6;
00051 break;
00052 case 15:
00053 Red = Green = Blue = 5;
00054 break;
00055 case 8:
00056 Red = Green = 3;
00057 Blue = 2;
00058 IsIndexed = true;
00059 break;
00060 case 4:
00061 Red = Green = 2;
00062 Blue = 1;
00063 IsIndexed = true;
00064 break;
00065 case 1:
00066 IsIndexed = true;
00067 break;
00068 default:
00069 Red = Blue = Alpha = (byte)(bpp / 4);
00070 Green = (byte)((bpp / 4) + (bpp % 4));
00071 break;
00072 }
00073 }
00074
00083 public ColorFormat(int red, int green, int blue, int alpha)
00084 {
00085 if (red < 0 || green < 0 || blue < 0 || alpha < 0)
00086 throw new ArgumentOutOfRangeException("Arguments must be greater or equal to zero.");
00087 this.red = (byte)red;
00088 this.green = (byte)green;
00089 this.blue = (byte)blue;
00090 this.alpha = (byte)alpha;
00091 this.bitsPerPixel = red + green + blue + alpha;
00092 this.isIndexed = false;
00093 if (this.bitsPerPixel < 15 && this.bitsPerPixel != 0)
00094 this.isIndexed = true;
00095 }
00096
00097 #endregion
00098
00099 #region --- Public Methods ---
00100
00102 public int Red { get { return red; } private set { red = (byte)value; } }
00104 public int Green { get { return green; } private set { green = (byte)value; } }
00106 public int Blue { get { return blue; } private set { blue = (byte)value; } }
00108 public int Alpha { get { return alpha; } private set { alpha = (byte)value; } }
00110 public bool IsIndexed { get { return isIndexed; } private set { isIndexed = value; } }
00112 public int BitsPerPixel { get { return bitsPerPixel; } private set { bitsPerPixel = value; } }
00113
00114 #endregion
00115
00116 #region --- Operator Overloads ---
00117
00123 public static implicit operator ColorFormat(int bpp)
00124 {
00125 return new ColorFormat(bpp);
00126 }
00127
00128
00129
00130
00131
00132
00133 #endregion
00134
00135 #region --- Overrides ---
00136
00142 public override bool Equals(object obj)
00143 {
00144 return (obj is ColorFormat) ? (this == (ColorFormat)obj) : false;
00145 }
00146
00153 public static bool operator ==(ColorFormat left, ColorFormat right)
00154 {
00155 if ((object)left == (object)null && (object)right != (object)null ||
00156 (object)left != (object)null && (object)right == (object)null)
00157 return false;
00158
00159 if ((object)left == (object)null && (object)right == (object)null)
00160 return true;
00161
00162 return left.Red == right.Red &&
00163 left.Green == right.Green &&
00164 left.Blue == right.Blue &&
00165 left.Alpha == right.Alpha;
00166 }
00167
00174 public static bool operator !=(ColorFormat left, ColorFormat right)
00175 {
00176 return !(left == right);
00177 }
00178
00183 public override int GetHashCode()
00184 {
00185 return Red ^ Green ^ Blue ^ Alpha;
00186 }
00187
00192 public override string ToString()
00193 {
00194 return string.Format("{0} ({1})", BitsPerPixel, (IsIndexed ? " indexed" : Red.ToString() + Green.ToString() + Blue.ToString() + Alpha.ToString()));
00195 }
00196
00197 #endregion
00198 }
00199 }