001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj.util;
006
007import edu.wpi.first.math.MathUtil;
008import java.util.Objects;
009
010/**
011 * Represents colors.
012 *
013 * <p>Limited to 12 bits of precision.
014 */
015@SuppressWarnings("MemberName")
016public class Color {
017  private static final double kPrecision = Math.pow(2, -12);
018
019  public final double red;
020  public final double green;
021  public final double blue;
022
023  /**
024   * Constructs a Color.
025   *
026   * @param red Red value (0-1)
027   * @param green Green value (0-1)
028   * @param blue Blue value (0-1)
029   */
030  public Color(double red, double green, double blue) {
031    this.red = roundAndClamp(red);
032    this.green = roundAndClamp(green);
033    this.blue = roundAndClamp(blue);
034  }
035
036  /**
037   * Constructs a Color from a Color8Bit.
038   *
039   * @param color The color
040   */
041  public Color(Color8Bit color) {
042    this(color.red / 255.0, color.green / 255.0, color.blue / 255.0);
043  }
044
045  /**
046   * Creates a Color from HSV values.
047   *
048   * @param h The h value [0-180]
049   * @param s The s value [0-255]
050   * @param v The v value [0-255]
051   * @return The color
052   */
053  @SuppressWarnings("ParameterName")
054  public static Color fromHSV(int h, int s, int v) {
055    if (s == 0) {
056      return new Color(v / 255.0, v / 255.0, v / 255.0);
057    }
058
059    final int region = h / 30;
060    final int remainder = (h - (region * 30)) * 6;
061
062    final int p = (v * (255 - s)) >> 8;
063    final int q = (v * (255 - ((s * remainder) >> 8))) >> 8;
064    final int t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
065
066    switch (region) {
067      case 0:
068        return new Color(v / 255.0, t / 255.0, p / 255.0);
069      case 1:
070        return new Color(q / 255.0, v / 255.0, p / 255.0);
071      case 2:
072        return new Color(p / 255.0, v / 255.0, t / 255.0);
073      case 3:
074        return new Color(p / 255.0, q / 255.0, v / 255.0);
075      case 4:
076        return new Color(t / 255.0, p / 255.0, v / 255.0);
077      default:
078        return new Color(v / 255.0, p / 255.0, q / 255.0);
079    }
080  }
081
082  @Override
083  public boolean equals(Object other) {
084    if (this == other) {
085      return true;
086    }
087    if (other == null || getClass() != other.getClass()) {
088      return false;
089    }
090
091    Color color = (Color) other;
092    return Double.compare(color.red, red) == 0
093        && Double.compare(color.green, green) == 0
094        && Double.compare(color.blue, blue) == 0;
095  }
096
097  @Override
098  public int hashCode() {
099    return Objects.hash(red, green, blue);
100  }
101
102  private static double roundAndClamp(double value) {
103    final var rounded = Math.round((value + kPrecision / 2) / kPrecision) * kPrecision;
104    return MathUtil.clamp(rounded, 0.0, 1.0);
105  }
106
107  /*
108   * FIRST Colors
109   */
110
111  /** 0x1560BD. */
112  public static final Color kDenim = new Color(0.0823529412, 0.376470589, 0.7411764706);
113
114  /** 0x0066B3. */
115  public static final Color kFirstBlue = new Color(0.0, 0.4, 0.7019607844);
116
117  /** 0xED1C24. */
118  public static final Color kFirstRed = new Color(0.9294117648, 0.1098039216, 0.1411764706);
119
120  /*
121   * Standard Colors
122   */
123
124  /** 0xF0F8FF. */
125  public static final Color kAliceBlue = new Color(0.9411765f, 0.972549f, 1.0f);
126
127  /** 0xFAEBD7. */
128  public static final Color kAntiqueWhite = new Color(0.98039216f, 0.92156863f, 0.84313726f);
129
130  /** 0x00FFFF. */
131  public static final Color kAqua = new Color(0.0f, 1.0f, 1.0f);
132
133  /** 0x7FFFD4. */
134  public static final Color kAquamarine = new Color(0.49803922f, 1.0f, 0.83137256f);
135
136  /** 0xF0FFFF. */
137  public static final Color kAzure = new Color(0.9411765f, 1.0f, 1.0f);
138
139  /** 0xF5F5DC. */
140  public static final Color kBeige = new Color(0.9607843f, 0.9607843f, 0.8627451f);
141
142  /** 0xFFE4C4. */
143  public static final Color kBisque = new Color(1.0f, 0.89411765f, 0.76862746f);
144
145  /** 0x000000. */
146  public static final Color kBlack = new Color(0.0f, 0.0f, 0.0f);
147
148  /** 0xFFEBCD. */
149  public static final Color kBlanchedAlmond = new Color(1.0f, 0.92156863f, 0.8039216f);
150
151  /** 0x0000FF. */
152  public static final Color kBlue = new Color(0.0f, 0.0f, 1.0f);
153
154  /** 0x8A2BE2. */
155  public static final Color kBlueViolet = new Color(0.5411765f, 0.16862746f, 0.8862745f);
156
157  /** 0xA52A2A. */
158  public static final Color kBrown = new Color(0.64705884f, 0.16470589f, 0.16470589f);
159
160  /** 0xDEB887. */
161  public static final Color kBurlywood = new Color(0.87058824f, 0.72156864f, 0.5294118f);
162
163  /** 0x5F9EA0. */
164  public static final Color kCadetBlue = new Color(0.37254903f, 0.61960787f, 0.627451f);
165
166  /** 0x7FFF00. */
167  public static final Color kChartreuse = new Color(0.49803922f, 1.0f, 0.0f);
168
169  /** 0xD2691E. */
170  public static final Color kChocolate = new Color(0.8235294f, 0.4117647f, 0.11764706f);
171
172  /** 0xFF7F50. */
173  public static final Color kCoral = new Color(1.0f, 0.49803922f, 0.3137255f);
174
175  /** 0x6495ED. */
176  public static final Color kCornflowerBlue = new Color(0.39215687f, 0.58431375f, 0.92941177f);
177
178  /** 0xFFF8DC. */
179  public static final Color kCornsilk = new Color(1.0f, 0.972549f, 0.8627451f);
180
181  /** 0xDC143C. */
182  public static final Color kCrimson = new Color(0.8627451f, 0.078431375f, 0.23529412f);
183
184  /** 0x00FFFF. */
185  public static final Color kCyan = new Color(0.0f, 1.0f, 1.0f);
186
187  /** 0x00008B. */
188  public static final Color kDarkBlue = new Color(0.0f, 0.0f, 0.54509807f);
189
190  /** 0x008B8B. */
191  public static final Color kDarkCyan = new Color(0.0f, 0.54509807f, 0.54509807f);
192
193  /** 0xB8860B. */
194  public static final Color kDarkGoldenrod = new Color(0.72156864f, 0.5254902f, 0.043137256f);
195
196  /** 0xA9A9A9. */
197  public static final Color kDarkGray = new Color(0.6627451f, 0.6627451f, 0.6627451f);
198
199  /** 0x006400. */
200  public static final Color kDarkGreen = new Color(0.0f, 0.39215687f, 0.0f);
201
202  /** 0xBDB76B. */
203  public static final Color kDarkKhaki = new Color(0.7411765f, 0.7176471f, 0.41960785f);
204
205  /** 0x8B008B. */
206  public static final Color kDarkMagenta = new Color(0.54509807f, 0.0f, 0.54509807f);
207
208  /** 0x556B2F. */
209  public static final Color kDarkOliveGreen = new Color(0.33333334f, 0.41960785f, 0.18431373f);
210
211  /** 0xFF8C00. */
212  public static final Color kDarkOrange = new Color(1.0f, 0.54901963f, 0.0f);
213
214  /** 0x9932CC. */
215  public static final Color kDarkOrchid = new Color(0.6f, 0.19607843f, 0.8f);
216
217  /** 0x8B0000. */
218  public static final Color kDarkRed = new Color(0.54509807f, 0.0f, 0.0f);
219
220  /** 0xE9967A. */
221  public static final Color kDarkSalmon = new Color(0.9137255f, 0.5882353f, 0.47843137f);
222
223  /** 0x8FBC8F. */
224  public static final Color kDarkSeaGreen = new Color(0.56078434f, 0.7372549f, 0.56078434f);
225
226  /** 0x483D8B. */
227  public static final Color kDarkSlateBlue = new Color(0.28235295f, 0.23921569f, 0.54509807f);
228
229  /** 0x2F4F4F. */
230  public static final Color kDarkSlateGray = new Color(0.18431373f, 0.30980393f, 0.30980393f);
231
232  /** 0x00CED1. */
233  public static final Color kDarkTurquoise = new Color(0.0f, 0.80784315f, 0.81960785f);
234
235  /** 0x9400D3. */
236  public static final Color kDarkViolet = new Color(0.5803922f, 0.0f, 0.827451f);
237
238  /** 0xFF1493. */
239  public static final Color kDeepPink = new Color(1.0f, 0.078431375f, 0.5764706f);
240
241  /** 0x00BFFF. */
242  public static final Color kDeepSkyBlue = new Color(0.0f, 0.7490196f, 1.0f);
243
244  /** 0x696969. */
245  public static final Color kDimGray = new Color(0.4117647f, 0.4117647f, 0.4117647f);
246
247  /** 0x1E90FF. */
248  public static final Color kDodgerBlue = new Color(0.11764706f, 0.5647059f, 1.0f);
249
250  /** 0xB22222. */
251  public static final Color kFirebrick = new Color(0.69803923f, 0.13333334f, 0.13333334f);
252
253  /** 0xFFFAF0. */
254  public static final Color kFloralWhite = new Color(1.0f, 0.98039216f, 0.9411765f);
255
256  /** 0x228B22. */
257  public static final Color kForestGreen = new Color(0.13333334f, 0.54509807f, 0.13333334f);
258
259  /** 0xFF00FF. */
260  public static final Color kFuchsia = new Color(1.0f, 0.0f, 1.0f);
261
262  /** 0xDCDCDC. */
263  public static final Color kGainsboro = new Color(0.8627451f, 0.8627451f, 0.8627451f);
264
265  /** 0xF8F8FF. */
266  public static final Color kGhostWhite = new Color(0.972549f, 0.972549f, 1.0f);
267
268  /** 0xFFD700. */
269  public static final Color kGold = new Color(1.0f, 0.84313726f, 0.0f);
270
271  /** 0xDAA520. */
272  public static final Color kGoldenrod = new Color(0.85490197f, 0.64705884f, 0.1254902f);
273
274  /** 0x808080. */
275  public static final Color kGray = new Color(0.5019608f, 0.5019608f, 0.5019608f);
276
277  /** 0x008000. */
278  public static final Color kGreen = new Color(0.0f, 0.5019608f, 0.0f);
279
280  /** 0xADFF2F. */
281  public static final Color kGreenYellow = new Color(0.6784314f, 1.0f, 0.18431373f);
282
283  /** 0xF0FFF0. */
284  public static final Color kHoneydew = new Color(0.9411765f, 1.0f, 0.9411765f);
285
286  /** 0xFF69B4. */
287  public static final Color kHotPink = new Color(1.0f, 0.4117647f, 0.7058824f);
288
289  /** 0xCD5C5C. */
290  public static final Color kIndianRed = new Color(0.8039216f, 0.36078432f, 0.36078432f);
291
292  /** 0x4B0082. */
293  public static final Color kIndigo = new Color(0.29411766f, 0.0f, 0.50980395f);
294
295  /** 0xFFFFF0. */
296  public static final Color kIvory = new Color(1.0f, 1.0f, 0.9411765f);
297
298  /** 0xF0E68C. */
299  public static final Color kKhaki = new Color(0.9411765f, 0.9019608f, 0.54901963f);
300
301  /** 0xE6E6FA. */
302  public static final Color kLavender = new Color(0.9019608f, 0.9019608f, 0.98039216f);
303
304  /** 0xFFF0F5. */
305  public static final Color kLavenderBlush = new Color(1.0f, 0.9411765f, 0.9607843f);
306
307  /** 0x7CFC00. */
308  public static final Color kLawnGreen = new Color(0.4862745f, 0.9882353f, 0.0f);
309
310  /** 0xFFFACD. */
311  public static final Color kLemonChiffon = new Color(1.0f, 0.98039216f, 0.8039216f);
312
313  /** 0xADD8E6. */
314  public static final Color kLightBlue = new Color(0.6784314f, 0.84705883f, 0.9019608f);
315
316  /** 0xF08080. */
317  public static final Color kLightCoral = new Color(0.9411765f, 0.5019608f, 0.5019608f);
318
319  /** 0xE0FFFF. */
320  public static final Color kLightCyan = new Color(0.8784314f, 1.0f, 1.0f);
321
322  /** 0xFAFAD2. */
323  public static final Color kLightGoldenrodYellow = new Color(0.98039216f, 0.98039216f, 0.8235294f);
324
325  /** 0xD3D3D3. */
326  public static final Color kLightGray = new Color(0.827451f, 0.827451f, 0.827451f);
327
328  /** 0x90EE90. */
329  public static final Color kLightGreen = new Color(0.5647059f, 0.93333334f, 0.5647059f);
330
331  /** 0xFFB6C1. */
332  public static final Color kLightPink = new Color(1.0f, 0.7137255f, 0.75686276f);
333
334  /** 0xFFA07A. */
335  public static final Color kLightSalmon = new Color(1.0f, 0.627451f, 0.47843137f);
336
337  /** 0x20B2AA. */
338  public static final Color kLightSeaGreen = new Color(0.1254902f, 0.69803923f, 0.6666667f);
339
340  /** 0x87CEFA. */
341  public static final Color kLightSkyBlue = new Color(0.5294118f, 0.80784315f, 0.98039216f);
342
343  /** 0x778899. */
344  public static final Color kLightSlateGray = new Color(0.46666667f, 0.53333336f, 0.6f);
345
346  /** 0xB0C4DE. */
347  public static final Color kLightSteelBlue = new Color(0.6901961f, 0.76862746f, 0.87058824f);
348
349  /** 0xFFFFE0. */
350  public static final Color kLightYellow = new Color(1.0f, 1.0f, 0.8784314f);
351
352  /** 0x00FF00. */
353  public static final Color kLime = new Color(0.0f, 1.0f, 0.0f);
354
355  /** 0x32CD32. */
356  public static final Color kLimeGreen = new Color(0.19607843f, 0.8039216f, 0.19607843f);
357
358  /** 0xFAF0E6. */
359  public static final Color kLinen = new Color(0.98039216f, 0.9411765f, 0.9019608f);
360
361  /** 0xFF00FF. */
362  public static final Color kMagenta = new Color(1.0f, 0.0f, 1.0f);
363
364  /** 0x800000. */
365  public static final Color kMaroon = new Color(0.5019608f, 0.0f, 0.0f);
366
367  /** 0x66CDAA. */
368  public static final Color kMediumAquamarine = new Color(0.4f, 0.8039216f, 0.6666667f);
369
370  /** 0x0000CD. */
371  public static final Color kMediumBlue = new Color(0.0f, 0.0f, 0.8039216f);
372
373  /** 0xBA55D3. */
374  public static final Color kMediumOrchid = new Color(0.7294118f, 0.33333334f, 0.827451f);
375
376  /** 0x9370DB. */
377  public static final Color kMediumPurple = new Color(0.5764706f, 0.4392157f, 0.85882354f);
378
379  /** 0x3CB371. */
380  public static final Color kMediumSeaGreen = new Color(0.23529412f, 0.7019608f, 0.44313726f);
381
382  /** 0x7B68EE. */
383  public static final Color kMediumSlateBlue = new Color(0.48235294f, 0.40784314f, 0.93333334f);
384
385  /** 0x00FA9A. */
386  public static final Color kMediumSpringGreen = new Color(0.0f, 0.98039216f, 0.6039216f);
387
388  /** 0x48D1CC. */
389  public static final Color kMediumTurquoise = new Color(0.28235295f, 0.81960785f, 0.8f);
390
391  /** 0xC71585. */
392  public static final Color kMediumVioletRed = new Color(0.78039217f, 0.08235294f, 0.52156866f);
393
394  /** 0x191970. */
395  public static final Color kMidnightBlue = new Color(0.09803922f, 0.09803922f, 0.4392157f);
396
397  /** 0xF5FFFA. */
398  public static final Color kMintcream = new Color(0.9607843f, 1.0f, 0.98039216f);
399
400  /** 0xFFE4E1. */
401  public static final Color kMistyRose = new Color(1.0f, 0.89411765f, 0.88235295f);
402
403  /** 0xFFE4B5. */
404  public static final Color kMoccasin = new Color(1.0f, 0.89411765f, 0.70980394f);
405
406  /** 0xFFDEAD. */
407  public static final Color kNavajoWhite = new Color(1.0f, 0.87058824f, 0.6784314f);
408
409  /** 0x000080. */
410  public static final Color kNavy = new Color(0.0f, 0.0f, 0.5019608f);
411
412  /** 0xFDF5E6. */
413  public static final Color kOldLace = new Color(0.99215686f, 0.9607843f, 0.9019608f);
414
415  /** 0x808000. */
416  public static final Color kOlive = new Color(0.5019608f, 0.5019608f, 0.0f);
417
418  /** 0x6B8E23. */
419  public static final Color kOliveDrab = new Color(0.41960785f, 0.5568628f, 0.13725491f);
420
421  /** 0xFFA500. */
422  public static final Color kOrange = new Color(1.0f, 0.64705884f, 0.0f);
423
424  /** 0xFF4500. */
425  public static final Color kOrangeRed = new Color(1.0f, 0.27058825f, 0.0f);
426
427  /** 0xDA70D6. */
428  public static final Color kOrchid = new Color(0.85490197f, 0.4392157f, 0.8392157f);
429
430  /** 0xEEE8AA. */
431  public static final Color kPaleGoldenrod = new Color(0.93333334f, 0.9098039f, 0.6666667f);
432
433  /** 0x98FB98. */
434  public static final Color kPaleGreen = new Color(0.59607846f, 0.9843137f, 0.59607846f);
435
436  /** 0xAFEEEE. */
437  public static final Color kPaleTurquoise = new Color(0.6862745f, 0.93333334f, 0.93333334f);
438
439  /** 0xDB7093. */
440  public static final Color kPaleVioletRed = new Color(0.85882354f, 0.4392157f, 0.5764706f);
441
442  /** 0xFFEFD5. */
443  public static final Color kPapayaWhip = new Color(1.0f, 0.9372549f, 0.8352941f);
444
445  /** 0xFFDAB9. */
446  public static final Color kPeachPuff = new Color(1.0f, 0.85490197f, 0.7254902f);
447
448  /** 0xCD853F. */
449  public static final Color kPeru = new Color(0.8039216f, 0.52156866f, 0.24705882f);
450
451  /** 0xFFC0CB. */
452  public static final Color kPink = new Color(1.0f, 0.7529412f, 0.79607844f);
453
454  /** 0xDDA0DD. */
455  public static final Color kPlum = new Color(0.8666667f, 0.627451f, 0.8666667f);
456
457  /** 0xB0E0E6. */
458  public static final Color kPowderBlue = new Color(0.6901961f, 0.8784314f, 0.9019608f);
459
460  /** 0x800080. */
461  public static final Color kPurple = new Color(0.5019608f, 0.0f, 0.5019608f);
462
463  /** 0xFF0000. */
464  public static final Color kRed = new Color(1.0f, 0.0f, 0.0f);
465
466  /** 0xBC8F8F. */
467  public static final Color kRosyBrown = new Color(0.7372549f, 0.56078434f, 0.56078434f);
468
469  /** 0x4169E1. */
470  public static final Color kRoyalBlue = new Color(0.25490198f, 0.4117647f, 0.88235295f);
471
472  /** 0x8B4513. */
473  public static final Color kSaddleBrown = new Color(0.54509807f, 0.27058825f, 0.07450981f);
474
475  /** 0xFA8072. */
476  public static final Color kSalmon = new Color(0.98039216f, 0.5019608f, 0.44705883f);
477
478  /** 0xF4A460. */
479  public static final Color kSandyBrown = new Color(0.95686275f, 0.6431373f, 0.3764706f);
480
481  /** 0x2E8B57. */
482  public static final Color kSeaGreen = new Color(0.18039216f, 0.54509807f, 0.34117648f);
483
484  /** 0xFFF5EE. */
485  public static final Color kSeashell = new Color(1.0f, 0.9607843f, 0.93333334f);
486
487  /** 0xA0522D. */
488  public static final Color kSienna = new Color(0.627451f, 0.32156864f, 0.1764706f);
489
490  /** 0xC0C0C0. */
491  public static final Color kSilver = new Color(0.7529412f, 0.7529412f, 0.7529412f);
492
493  /** 0x87CEEB. */
494  public static final Color kSkyBlue = new Color(0.5294118f, 0.80784315f, 0.92156863f);
495
496  /** 0x6A5ACD. */
497  public static final Color kSlateBlue = new Color(0.41568628f, 0.3529412f, 0.8039216f);
498
499  /** 0x708090. */
500  public static final Color kSlateGray = new Color(0.4392157f, 0.5019608f, 0.5647059f);
501
502  /** 0xFFFAFA. */
503  public static final Color kSnow = new Color(1.0f, 0.98039216f, 0.98039216f);
504
505  /** 0x00FF7F. */
506  public static final Color kSpringGreen = new Color(0.0f, 1.0f, 0.49803922f);
507
508  /** 0x4682B4. */
509  public static final Color kSteelBlue = new Color(0.27450982f, 0.50980395f, 0.7058824f);
510
511  /** 0xD2B48C. */
512  public static final Color kTan = new Color(0.8235294f, 0.7058824f, 0.54901963f);
513
514  /** 0x008080. */
515  public static final Color kTeal = new Color(0.0f, 0.5019608f, 0.5019608f);
516
517  /** 0xD8BFD8. */
518  public static final Color kThistle = new Color(0.84705883f, 0.7490196f, 0.84705883f);
519
520  /** 0xFF6347. */
521  public static final Color kTomato = new Color(1.0f, 0.3882353f, 0.2784314f);
522
523  /** 0x40E0D0. */
524  public static final Color kTurquoise = new Color(0.2509804f, 0.8784314f, 0.8156863f);
525
526  /** 0xEE82EE. */
527  public static final Color kViolet = new Color(0.93333334f, 0.50980395f, 0.93333334f);
528
529  /** 0xF5DEB3. */
530  public static final Color kWheat = new Color(0.9607843f, 0.87058824f, 0.7019608f);
531
532  /** 0xFFFFFF. */
533  public static final Color kWhite = new Color(1.0f, 1.0f, 1.0f);
534
535  /** 0xF5F5F5. */
536  public static final Color kWhiteSmoke = new Color(0.9607843f, 0.9607843f, 0.9607843f);
537
538  /** 0xFFFF00. */
539  public static final Color kYellow = new Color(1.0f, 1.0f, 0.0f);
540
541  /** 0x9ACD32. */
542  public static final Color kYellowGreen = new Color(0.6039216f, 0.8039216f, 0.19607843f);
543}