Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Licensed under the MIT/X11 license. |
||
| 3 | * Copyright (c) 2006-2008 the OpenTK Team. |
||
| 4 | * This notice may not be removed from any source distribution. |
||
| 5 | * See license.txt for licensing detailed licensing details. |
||
| 6 | */ |
||
| 7 | #endregion |
||
| 8 | |||
| 9 | using System; |
||
| 10 | using System.Collections.Generic; |
||
| 11 | using System.Text; |
||
| 12 | |||
| 13 | namespace OpenTK.Graphics |
||
| 14 | { |
||
| 15 | /// <summary>Defines the ColorFormat component of a GraphicsMode.</summary> |
||
| 16 | /// <remarks> |
||
| 17 | /// <para>A ColorFormat contains Red, Green, Blue and Alpha components that descibe |
||
| 18 | /// the allocated bits per pixel for the corresponding color.</para> |
||
| 19 | /// </remarks> |
||
| 20 | public struct ColorFormat |
||
| 21 | { |
||
| 22 | byte red, green, blue, alpha; |
||
| 23 | bool isIndexed; |
||
| 24 | int bitsPerPixel; |
||
| 25 | |||
| 26 | #region --- Constructors --- |
||
| 27 | |||
| 28 | /// <summary> |
||
| 29 | /// Constructs a new ColorFormat with the specified aggregate bits per pixel. |
||
| 30 | /// </summary> |
||
| 31 | /// <param name="bpp">The bits per pixel sum for the Red, Green, Blue and Alpha color channels.</param> |
||
| 32 | public ColorFormat(int bpp) |
||
| 33 | { |
||
| 34 | if (bpp < 0) |
||
| 35 | throw new ArgumentOutOfRangeException("bpp", "Must be greater or equal to zero."); |
||
| 36 | red = green = blue = alpha = 0; |
||
| 37 | bitsPerPixel = bpp; |
||
| 38 | isIndexed = false; |
||
| 39 | |||
| 40 | switch (bpp) |
||
| 41 | { |
||
| 42 | case 32: |
||
| 43 | Red = Green = Blue = Alpha = 8; |
||
| 44 | break; |
||
| 45 | case 24: |
||
| 46 | Red = Green = Blue = 8; |
||
| 47 | break; |
||
| 48 | case 16: |
||
| 49 | Red = Blue = 5; |
||
| 50 | Green = 6; |
||
| 51 | break; |
||
| 52 | case 15: |
||
| 53 | Red = Green = Blue = 5; |
||
| 54 | break; |
||
| 55 | case 8: |
||
| 56 | Red = Green = 3; |
||
| 57 | Blue = 2; |
||
| 58 | IsIndexed = true; |
||
| 59 | break; |
||
| 60 | case 4: |
||
| 61 | Red = Green = 2; |
||
| 62 | Blue = 1; |
||
| 63 | IsIndexed = true; |
||
| 64 | break; |
||
| 65 | case 1: |
||
| 66 | IsIndexed = true; |
||
| 67 | break; |
||
| 68 | default: |
||
| 69 | Red = Blue = Alpha = (byte)(bpp / 4); |
||
| 70 | Green = (byte)((bpp / 4) + (bpp % 4)); |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /// <summary> |
||
| 76 | /// Constructs a new ColorFormat with the specified bits per pixel for |
||
| 77 | /// the Red, Green, Blue and Alpha color channels. |
||
| 78 | /// </summary> |
||
| 79 | /// <param name="red">Bits per pixel for the Red color channel.</param> |
||
| 80 | /// <param name="green">Bits per pixel for the Green color channel.</param> |
||
| 81 | /// <param name="blue">Bits per pixel for the Blue color channel.</param> |
||
| 82 | /// <param name="alpha">Bits per pixel for the Alpha color channel.</param> |
||
| 83 | public ColorFormat(int red, int green, int blue, int alpha) |
||
| 84 | { |
||
| 85 | if (red < 0 || green < 0 || blue < 0 || alpha < 0) |
||
| 86 | throw new ArgumentOutOfRangeException("Arguments must be greater or equal to zero."); |
||
| 87 | this.red = (byte)red; |
||
| 88 | this.green = (byte)green; |
||
| 89 | this.blue = (byte)blue; |
||
| 90 | this.alpha = (byte)alpha; |
||
| 91 | this.bitsPerPixel = red + green + blue + alpha; |
||
| 92 | this.isIndexed = false; |
||
| 93 | if (this.bitsPerPixel < 15 && this.bitsPerPixel != 0) |
||
| 94 | this.isIndexed = true; |
||
| 95 | } |
||
| 96 | |||
| 97 | #endregion |
||
| 98 | |||
| 99 | #region --- Public Methods --- |
||
| 100 | |||
| 101 | /// <summary>Gets the bits per pixel for the Red channel.</summary> |
||
| 102 | public int Red { get { return red; } private set { red = (byte)value; } } |
||
| 103 | /// <summary>Gets the bits per pixel for the Green channel.</summary> |
||
| 104 | public int Green { get { return green; } private set { green = (byte)value; } } |
||
| 105 | /// <summary>Gets the bits per pixel for the Blue channel.</summary> |
||
| 106 | public int Blue { get { return blue; } private set { blue = (byte)value; } } |
||
| 107 | /// <summary>Gets the bits per pixel for the Alpha channel.</summary> |
||
| 108 | public int Alpha { get { return alpha; } private set { alpha = (byte)value; } } |
||
| 109 | /// <summary>Gets a System.Boolean indicating whether this ColorFormat is indexed.</summary> |
||
| 110 | public bool IsIndexed { get { return isIndexed; } private set { isIndexed = value; } } |
||
| 111 | /// <summary>Gets the sum of Red, Green, Blue and Alpha bits per pixel.</summary> |
||
| 112 | public int BitsPerPixel { get { return bitsPerPixel; } private set { bitsPerPixel = value; } } |
||
| 113 | |||
| 114 | #endregion |
||
| 115 | |||
| 116 | #region --- Operator Overloads --- |
||
| 117 | |||
| 118 | /// <summary> |
||
| 119 | /// Converts the specified bpp into a new ColorFormat. |
||
| 120 | /// </summary> |
||
| 121 | /// <param name="bpp">The bits per pixel to convert.</param> |
||
| 122 | /// <returns>A ColorFormat with the specified bits per pixel.</returns> |
||
| 123 | public static implicit operator ColorFormat(int bpp) |
||
| 124 | { |
||
| 125 | return new ColorFormat(bpp); |
||
| 126 | } |
||
| 127 | |||
| 128 | //public static implicit operator int(ColorFormat mode) |
||
| 129 | //{ |
||
| 130 | // return mode.BitsPerPixel; |
||
| 131 | //} |
||
| 132 | |||
| 133 | #endregion |
||
| 134 | |||
| 135 | #region --- Overrides --- |
||
| 136 | |||
| 137 | /// <summary> |
||
| 138 | /// Indicates whether this instance and a specified object are equal. |
||
| 139 | /// </summary> |
||
| 140 | /// <param name="obj">Another object to compare to.</param> |
||
| 141 | /// <returns>True if this instance is equal to obj; false otherwise.</returns> |
||
| 142 | public override bool Equals(object obj) |
||
| 143 | { |
||
| 144 | return (obj is ColorFormat) ? (this == (ColorFormat)obj) : false; |
||
| 145 | } |
||
| 146 | |||
| 147 | /// <summary> |
||
| 148 | /// Compares two instances for equality. |
||
| 149 | /// </summary> |
||
| 150 | /// <param name="left">The left operand.</param> |
||
| 151 | /// <param name="right">The right operand.</param> |
||
| 152 | /// <returns>True if both instances are equal; false otherwise.</returns> |
||
| 153 | public static bool operator ==(ColorFormat left, ColorFormat right) |
||
| 154 | { |
||
| 155 | if ((object)left == (object)null && (object)right != (object)null || |
||
| 156 | (object)left != (object)null && (object)right == (object)null) |
||
| 157 | return false; |
||
| 158 | |||
| 159 | if ((object)left == (object)null && (object)right == (object)null) |
||
| 160 | return true; |
||
| 161 | |||
| 162 | return left.Red == right.Red && |
||
| 163 | left.Green == right.Green && |
||
| 164 | left.Blue == right.Blue && |
||
| 165 | left.Alpha == right.Alpha; |
||
| 166 | } |
||
| 167 | |||
| 168 | /// <summary> |
||
| 169 | /// Compares two instances for inequality. |
||
| 170 | /// </summary> |
||
| 171 | /// <param name="left">The left operand.</param> |
||
| 172 | /// <param name="right">The right operand.</param> |
||
| 173 | /// <returns>True if both instances are not equal; false otherwise.</returns> |
||
| 174 | public static bool operator !=(ColorFormat left, ColorFormat right) |
||
| 175 | { |
||
| 176 | return !(left == right); |
||
| 177 | } |
||
| 178 | |||
| 179 | /// <summary> |
||
| 180 | /// Returns the hash code for this instance. |
||
| 181 | /// </summary> |
||
| 182 | /// <returns>A System.Int32 with the hash code of this instance.</returns> |
||
| 183 | public override int GetHashCode() |
||
| 184 | { |
||
| 185 | return Red ^ Green ^ Blue ^ Alpha; |
||
| 186 | } |
||
| 187 | |||
| 188 | /// <summary> |
||
| 189 | /// Returns a <see cref="System.String"/> that describes this instance. |
||
| 190 | /// </summary> |
||
| 191 | /// <returns>A <see cref="System.String"/> that describes this instance.</returns> |
||
| 192 | public override string ToString() |
||
| 193 | { |
||
| 194 | return string.Format("{0} ({1})", BitsPerPixel, (IsIndexed ? " indexed" : Red.ToString() + Green.ToString() + Blue.ToString() + Alpha.ToString())); |
||
| 195 | } |
||
| 196 | |||
| 197 | #endregion |
||
| 198 | } |
||
| 199 | } |