summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/Color.cs
blob: 9b27f0ff38d08a7550b2785d5943658cc0a0db8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Globalization;

namespace ElmSharp
{
    /// <summary>
    /// The Color is a struct to record Check's state.
    /// </summary>
    public struct Color
    {
        readonly int _a;
        readonly int _r;
        readonly int _g;
        readonly int _b;

        readonly Mode _mode;

        enum Mode
        {
            Default,
            Rgb
        }

        /// <summary>
        /// Gets a default Color instance.
        /// </summary>
        /// <remarks>
        /// In default Color instance,Mode type is Default,RGBA all set as -1.
        /// </remarks>
        public static Color Default
        {
            get { return new Color(-1, -1, -1, -1, Mode.Default); }
        }

        /// <summary>
        /// Gets whether the Color instance's mode is default or not.
        /// Return type is bool.
        /// </summary>
        public bool IsDefault
        {
            get { return _mode == Mode.Default; }
        }

        /// <summary>
        /// Gets A value of RGBA.
        /// A means the Alpha in color.
        /// </summary>
        public int A
        {
            get { return _a; }
        }

        /// <summary>
        /// Gets R value of RGBA.
        /// R means the Red in color.
        /// </summary>
        public int R
        {
            get { return _r; }
        }

        /// <summary>
        /// Gets G value of RGBA.
        /// G means the Green in color.
        /// </summary>
        public int G
        {
            get { return _g; }
        }

        /// <summary>
        /// Gets B value of RGBA.
        /// B means the Blue in color.
        /// </summary>
        public int B
        {
            get { return _b; }
        }

        /// <summary>
        /// Creates and initializes a new instance of the Color class.
        /// With RGB parameters.
        /// </summary>
        /// <param name="r">Red of RGB</param>
        /// <param name="g">Green of RGB</param>
        /// <param name="b">Blue of RGB</param>
        public Color(int r, int g, int b) : this(r, g, b, 255)
        {
        }

        /// <summary>
        /// Creates and initializes a new instance of the Color class.
        /// With RGBA parameters.
        /// </summary>
        /// <param name="r">Red of RGBA</param>
        /// <param name="g">Green of RGBA<</param>
        /// <param name="b">Blue of RGBA<</param>
        /// <param name="a">Alpha of RGBA<</param>
        public Color(int r, int g, int b, int a) : this(r, g, b, a, Mode.Rgb)
        {
        }

        Color(int r, int g, int b, int a, Mode mode)
        {
            _mode = mode;
            if (mode == Mode.Rgb)
            {
                _r = Clamp(r, 0, 255);
                _g = Clamp(g, 0, 255);
                _b = Clamp(b, 0, 255);
                _a = Clamp(a, 0, 255);
            }
            else // Default
            {
                _r = _g = _b = _a = -1;
            }
        }

        public override int GetHashCode()
        {
            int hashcode = _r.GetHashCode();
            hashcode = (hashcode * 397) ^ _g.GetHashCode();
            hashcode = (hashcode * 397) ^ _b.GetHashCode();
            hashcode = (hashcode * 397) ^ _a.GetHashCode();
            return hashcode;
        }

        public override bool Equals(object obj)
        {
            if (obj is Color)
            {
                return EqualsInner(this, (Color)obj);
            }
            return base.Equals(obj);
        }

        /// <summary>
        /// Compare whether two Color instance is same or not.
        /// </summary>
        /// <param name="a">A Color instance.</param>
        /// <param name="b">A Color instance.</param>
        /// <returns>The result whether two instance is same or not.
        /// Return type is bool.If they are same, return true.
        /// </returns>
        public static bool operator ==(Color a, Color b)
        {
            if (ReferenceEquals(a, b))
                return true;

            if ((object)a == null || (object)b == null)
                return false;

            return EqualsInner(a, b);
        }

        /// <summary>
        /// Compare whether two Color instance is different or not.
        /// </summary>
        /// <param name="a">A Color instance.</param>
        /// <param name="b">A Color instance.</param>
        /// <returns>The result whether two instance is different or not.
        /// Return type is bool.If they are different, return true.
        /// </returns>
        public static bool operator !=(Color a, Color b)
        {
            return !(a == b);
        }

        static bool EqualsInner(Color color1, Color color2)
        {
            if (color1._mode == Mode.Default && color2._mode == Mode.Default)
                return true;
            return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
        }

        /// <summary>
        /// Gets a Color instance with a hexadecimal string parameter.
        /// </summary>
        /// <param name="hex">Hexadecimal string.</param>
        /// <returns>New instance of Color struct.</returns>
        public static Color FromHex(string hex)
        {
            hex = hex.Replace("#", "");
            switch (hex.Length)
            {
                case 3: //#rgb => ffrrggbb
                    hex = string.Format("ff{0}{1}{2}{3}{4}{5}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);
                    break;
                case 4: //#argb => aarrggbb
                    hex = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]);
                    break;
                case 6: //#rrggbb => ffrrggbb
                    hex = string.Format("ff{0}", hex);
                    break;
            }
            return FromUint(Convert.ToUInt32(hex.Replace("#", ""), 16));
        }

        /// <summary>
        /// Gets a Color instance with a Unsigned integer parameter.
        /// </summary>
        /// <param name="argb">Unsigned integer indicates RGBA.</param>
        /// <returns>New instance of Color struct.</returns>
        public static Color FromUint(uint argb)
        {
            return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
        }

        /// <summary>
        /// Gets a Color instance with R,G,B,A parameters.
        /// </summary>
        /// <param name="r">Red in RGBA.</param>
        /// <param name="g">Green in RGBA.</param>
        /// <param name="b">Blue in RGBA.</param>
        /// <param name="a">Alpha in RGBA.</param>
        /// <returns>New instance of Color struct.</returns>
        public static Color FromRgba(int r, int g, int b, int a)
        {
            return new Color(r, g, b, a);
        }

        /// <summary>
        /// Gets a Color instance with R,G,B,A parameters.
        /// </summary>
        /// <param name="r">Red in RGB.</param>
        /// <param name="g">Green in RGB.</param>
        /// <param name="b">Blue in RGB.</param>
        /// <returns>New instance of Color struct.</returns>
        public static Color FromRgb(int r, int g, int b)
        {
            return FromRgba(r, g, b, 255);
        }

        internal static int Clamp(int self, int min, int max)
        {
            return Math.Min(max, Math.Max(self, min));
        }

        #region Color Definitions
        /// <summary>
        /// The Tansparent is a predefined Color, it's rgba value is (0, 0, 0, 0).
        /// </summary>
        public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
        /// <summary>
        /// The Aqua is a predefined Color instance, it's rgb value is (0, 255, 255).
        /// </summary>
        public static readonly Color Aqua = FromRgb(0, 255, 255);
        /// <summary>
        /// The Black is a predefined Color instance, it's rgb value is (0, 0, 0).
        /// </summary>
        public static readonly Color Black = FromRgb(0, 0, 0);
        /// <summary>
        /// The Blue is a predefined Color instance, it's rgb value is (0, 0, 255).
        /// </summary>
        public static readonly Color Blue = FromRgb(0, 0, 255);
        /// <summary>
        /// The Fuchsia is a predefined Color instance, it's rgb value is (255, 0, 255).
        /// </summary>
        public static readonly Color Fuchsia = FromRgb(255, 0, 255);
        /// <summary>
        /// The Gray is a predefined Color instance, it's rgb value is (128, 128, 128).
        /// </summary>
        public static readonly Color Gray = FromRgb(128, 128, 128);
        /// <summary>
        /// The Green is a predefined Color instance, it's rgb value is (0, 128, 0).
        /// </summary>
        public static readonly Color Green = FromRgb(0, 128, 0);
        /// <summary>
        /// The Lime is a predefined Color instance, it's rgb value is (0, 255, 0).
        /// </summary>
        public static readonly Color Lime = FromRgb(0, 255, 0);
        /// <summary>
        /// The Maroon is a predefined Color instance, it's rgb value is (128, 0, 0).
        /// </summary>
        public static readonly Color Maroon = FromRgb(128, 0, 0);
        /// <summary>
        /// The Navy is a predefined Color instance, it's rgb value is (0, 0, 128).
        /// </summary>
        public static readonly Color Navy = FromRgb(0, 0, 128);
        /// <summary>
        /// The Olive is a predefined Color instance, it's rgb value is (128, 128, 0).
        /// </summary>
        public static readonly Color Olive = FromRgb(128, 128, 0);
        /// <summary>
        /// The Orange is a predefined Color instance, it's rgb value is (255, 165, 0).
        /// </summary>
        public static readonly Color Orange = FromRgb(255, 165, 0);
        /// <summary>
        /// The Purple is a predefined Color instance, it's rgb value is (128, 0, 128).
        /// </summary>
        public static readonly Color Purple = FromRgb(128, 0, 128);
        /// <summary>
        /// The Pink is a predefined Color instance, it's rgb value is (255, 102, 255).
        /// </summary>
        public static readonly Color Pink = FromRgb(255, 102, 255);
        /// <summary>
        /// The Red is a predefined Color instance, it's rgb value is (255, 0, 0).
        /// </summary>
        public static readonly Color Red = FromRgb(255, 0, 0);
        /// <summary>
        /// The Silver is a predefined Color instance, it's rgb value is (192, 192, 192).
        /// </summary>
        public static readonly Color Silver = FromRgb(192, 192, 192);
        /// <summary>
        /// The Teal is a predefined Color instance, it's rgb value is (0, 128, 128).
        /// </summary>
        public static readonly Color Teal = FromRgb(0, 128, 128);
        /// <summary>
        /// The White is a predefined Color instance, it's rgb value is (255, 255, 255).
        /// </summary>
        public static readonly Color White = FromRgb(255, 255, 255);
        /// <summary>
        /// The Yellow is a predefined Color instance, it's rgb value is (255, 255, 0).
        /// </summary>
        public static readonly Color Yellow = FromRgb(255, 255, 0);
        #endregion
    }
}