| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | | using System.Drawing; |
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | |
|
| | 5 | | namespace Imagini.Drawing |
| | 6 | | { |
| | 7 | | /* |
| | 8 | | * Note: When implementing SDL2 pixel formats, it should be noted that the |
| | 9 | | * channel order is reversed, i.e. RGBA means that A comes first, then B, |
| | 10 | | * then G, then R. |
| | 11 | | */ |
| | 12 | |
|
| | 13 | | public interface IColor |
| | 14 | | { |
| | 15 | | PixelFormat Format { get; } |
| | 16 | | Color AsColor(); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | [StructLayout(LayoutKind.Sequential)] |
| | 20 | | public struct ColorRGBA8888 : IColor |
| | 21 | | { |
| 1211 | 22 | | public byte A { get; set; } |
| 1209 | 23 | | public byte B { get; set; } |
| 1209 | 24 | | public byte G { get; set; } |
| 1209 | 25 | | public byte R { get; set; } |
| | 26 | |
|
| 9 | 27 | | public PixelFormat Format => PixelFormat.Format_RGBA8888; |
| | 28 | |
|
| | 29 | | public ColorRGBA8888(Color color) |
| | 30 | | { |
| 7 | 31 | | A = color.A; |
| 7 | 32 | | R = color.R; |
| 7 | 33 | | G = color.G; |
| 7 | 34 | | B = color.B; |
| 7 | 35 | | } |
| | 36 | |
|
| | 37 | | private bool Equals(ColorRGBA8888 other) |
| | 38 | | { |
| 501 | 39 | | return A == other.A && R == other.R && G == other.G && B == other.B; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Checks for object equality. |
| | 44 | | /// </summary> |
| | 45 | | public override bool Equals(object obj) |
| | 46 | | { |
| 501 | 47 | | if (obj is ColorRGBA8888) |
| 501 | 48 | | return Equals((ColorRGBA8888)obj); |
| 0 | 49 | | return false; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Calculates the hash code. |
| | 54 | | /// </summary> |
| | 55 | | public override int GetHashCode() |
| | 56 | | { |
| | 57 | | unchecked { |
| 100 | 58 | | return (int)((R << 24) | (G << 16) | (B << 8) | A); |
| | 59 | | } |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Converts to System.Drawing.Color. |
| | 64 | | /// </summary> |
| 102 | 65 | | public Color AsColor() => Color.FromArgb(A, R, G, B); |
| | 66 | |
|
| | 67 | | [ExcludeFromCodeCoverage] |
| | 68 | | /// <summary> |
| | 69 | | /// Converts from System.Drawing.Color. |
| | 70 | | /// </summary> |
| | 71 | | public static explicit operator ColorRGBA8888(Color clr) => new ColorRGBA8888(clr); |
| | 72 | | [ExcludeFromCodeCoverage] |
| | 73 | | /// <summary> |
| | 74 | | /// Converts to System.Drawing.Color. |
| | 75 | | /// </summary> |
| | 76 | | public static explicit operator Color(ColorRGBA8888 clr) => clr.AsColor(); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | [StructLayout(LayoutKind.Sequential)] |
| | 80 | | public struct ColorARGB8888 : IColor |
| | 81 | | { |
| | 82 | | public byte B { get; set; } |
| | 83 | | public byte G { get; set; } |
| | 84 | | public byte R { get; set; } |
| | 85 | | public byte A { get; set; } |
| | 86 | |
|
| | 87 | | public ColorARGB8888(Color color) |
| | 88 | | { |
| | 89 | | A = color.A; |
| | 90 | | R = color.R; |
| | 91 | | G = color.G; |
| | 92 | | B = color.B; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public PixelFormat Format => PixelFormat.Format_ARGB8888; |
| | 96 | |
|
| | 97 | | private bool Equals(ColorARGB8888 other) |
| | 98 | | { |
| | 99 | | return A == other.A && R == other.R && G == other.G && B == other.B; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Checks for object equality. |
| | 104 | | /// </summary> |
| | 105 | | public override bool Equals(object obj) |
| | 106 | | { |
| | 107 | | if (obj is ColorARGB8888) |
| | 108 | | return Equals((ColorARGB8888)obj); |
| | 109 | | return false; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Calculates the hash code. |
| | 114 | | /// </summary> |
| | 115 | | public override int GetHashCode() |
| | 116 | | { |
| | 117 | | unchecked { |
| | 118 | | return (int)((A << 24) | (R << 16) | (G << 8) | B); |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Converts to System.Drawing.Color. |
| | 124 | | /// </summary> |
| | 125 | | public Color AsColor() => Color.FromArgb(A, R, G, B); |
| | 126 | |
|
| | 127 | | [ExcludeFromCodeCoverage] |
| | 128 | | /// <summary> |
| | 129 | | /// Converts from System.Drawing.Color. |
| | 130 | | /// </summary> |
| | 131 | | public static explicit operator ColorARGB8888(Color clr) => new ColorARGB8888(clr); |
| | 132 | | [ExcludeFromCodeCoverage] |
| | 133 | | /// <summary> |
| | 134 | | /// Converts to System.Drawing.Color. |
| | 135 | | /// </summary> |
| | 136 | | public static explicit operator Color(ColorARGB8888 clr) => clr.AsColor(); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | [StructLayout(LayoutKind.Sequential)] |
| | 140 | | public struct ColorRGB888 : IColor |
| | 141 | | { |
| | 142 | | public byte B { get; set; } |
| | 143 | | public byte G { get; set; } |
| | 144 | | public byte R { get; set; } |
| | 145 | | byte Padding; |
| | 146 | |
|
| | 147 | | public ColorRGB888(Color color) |
| | 148 | | { |
| | 149 | | R = color.R; |
| | 150 | | G = color.G; |
| | 151 | | B = color.B; |
| | 152 | | Padding = 0; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | public PixelFormat Format => PixelFormat.Format_RGB888; |
| | 156 | |
|
| | 157 | | private bool Equals(ColorRGB888 other) |
| | 158 | | { |
| | 159 | | return R == other.R && G == other.G && B == other.B; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Checks for object equality. |
| | 164 | | /// </summary> |
| | 165 | | public override bool Equals(object obj) |
| | 166 | | { |
| | 167 | | if (obj is ColorRGB888) |
| | 168 | | return Equals((ColorRGB888)obj); |
| | 169 | | return false; |
| | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// Calculates the hash code. |
| | 174 | | /// </summary> |
| | 175 | | public override int GetHashCode() |
| | 176 | | { |
| | 177 | | unchecked { |
| | 178 | | return (int)((0xFF << 24) | (R << 16) | (G << 8) | B); |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Converts to System.Drawing.Color. |
| | 184 | | /// </summary> |
| | 185 | | public Color AsColor() => Color.FromArgb(255, R, G, B); |
| | 186 | |
|
| | 187 | | [ExcludeFromCodeCoverage] |
| | 188 | | /// <summary> |
| | 189 | | /// Converts from System.Drawing.Color. |
| | 190 | | /// </summary> |
| | 191 | | public static explicit operator ColorRGB888(Color clr) => new ColorRGB888(clr); |
| | 192 | | [ExcludeFromCodeCoverage] |
| | 193 | | /// <summary> |
| | 194 | | /// Converts to System.Drawing.Color. |
| | 195 | | /// </summary> |
| | 196 | | public static explicit operator Color(ColorRGB888 clr) => clr.AsColor(); |
| | 197 | | } |
| | 198 | |
|
| | 199 | | // TODO: Add more color types |
| | 200 | | } |