From e874ba9313a8243aa18eefb13e1d88a3999dd80e Mon Sep 17 00:00:00 2001 From: Prateek Machiraju Date: Mon, 30 Dec 2019 01:01:20 -0500 Subject: [PATCH] Add Color classes for use with AddressableLED (#2127) Both floating point and 8-bit integer classes are included, as well as a wide selection of color constants. Co-authored-by: Austin Shalit --- .../main/native/include/frc/AddressableLED.h | 24 + .../src/main/native/include/frc/util/Color.h | 949 ++++++++++++++++++ .../main/native/include/frc/util/Color8Bit.h | 58 ++ .../first/wpilibj/AddressableLEDBuffer.java | 38 +- .../edu/wpi/first/wpilibj/util/Color.java | 798 +++++++++++++++ .../edu/wpi/first/wpilibj/util/Color8Bit.java | 66 ++ .../edu/wpi/first/wpiutil/math/MathUtil.java | 11 + 7 files changed, 1938 insertions(+), 6 deletions(-) create mode 100644 wpilibc/src/main/native/include/frc/util/Color.h create mode 100644 wpilibc/src/main/native/include/frc/util/Color8Bit.h create mode 100644 wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java create mode 100644 wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java diff --git a/wpilibc/src/main/native/include/frc/AddressableLED.h b/wpilibc/src/main/native/include/frc/AddressableLED.h index 2ffe0ea382..5534ae4dd1 100644 --- a/wpilibc/src/main/native/include/frc/AddressableLED.h +++ b/wpilibc/src/main/native/include/frc/AddressableLED.h @@ -15,6 +15,8 @@ #include #include "frc/ErrorBase.h" +#include "util/Color.h" +#include "util/Color8Bit.h" namespace frc { @@ -54,6 +56,28 @@ class AddressableLED : public ErrorBase { * @param v the v value [0-255] */ void SetHSV(int h, int s, int v); + + /* + * Sets a specific LED in the buffer. + * + * @param color The color of the LED + */ + void SetLED(const Color& color) { + this->r = color.red * 255; + this->g = color.green * 255; + this->b = color.blue * 255; + } + + /* + * Sets a specific LED in the buffer. + * + * @param color The color of the LED + */ + void SetLED(const Color8Bit& color) { + this->r = color.red; + this->g = color.green; + this->b = color.blue; + } }; /** diff --git a/wpilibc/src/main/native/include/frc/util/Color.h b/wpilibc/src/main/native/include/frc/util/Color.h new file mode 100644 index 0000000000..2538d60ca8 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/util/Color.h @@ -0,0 +1,949 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +namespace frc { + +/** + * Represents colors that can be used with Addressable LEDs. + * + * Limited to 12 bits of precision. + */ +class Color { + public: + /* + * FIRST Colors + */ + + /** + * #1560BD. + */ + static const Color kDenim; + + /** + * #0066B3. + */ + static const Color kFirstBlue; + + /** + * #ED1C24. + */ + static const Color kFirstRed; + + /* + * Standard Colors + */ + + /** + * #F0F8FF. + */ + static const Color kAliceBlue; + + /** + * #FAEBD7. + */ + static const Color kAntiqueWhite; + + /** + * #00FFFF. + */ + static const Color kAqua; + + /** + * #7FFFD4. + */ + static const Color kAquamarine; + + /** + * #F0FFFF. + */ + static const Color kAzure; + + /** + * #F5F5DC. + */ + static const Color kBeige; + + /** + * #FFE4C4. + */ + static const Color kBisque; + + /** + * #000000. + */ + static const Color kBlack; + + /** + * #FFEBCD. + */ + static const Color kBlanchedAlmond; + + /** + * #0000FF. + */ + static const Color kBlue; + + /** + * #8A2BE2. + */ + static const Color kBlueViolet; + + /** + * #A52A2A. + */ + static const Color kBrown; + + /** + * #DEB887. + */ + static const Color kBurlywood; + + /** + * #5F9EA0. + */ + static const Color kCadetBlue; + + /** + * #7FFF00. + */ + static const Color kChartreuse; + + /** + * #D2691E. + */ + static const Color kChocolate; + + /** + * #FF7F50. + */ + static const Color kCoral; + + /** + * #6495ED. + */ + static const Color kCornflowerBlue; + + /** + * #FFF8DC. + */ + static const Color kCornsilk; + + /** + * #DC143C. + */ + static const Color kCrimson; + + /** + * #00FFFF. + */ + static const Color kCyan; + + /** + * #00008B. + */ + static const Color kDarkBlue; + + /** + * #008B8B. + */ + static const Color kDarkCyan; + + /** + * #B8860B. + */ + static const Color kDarkGoldenrod; + + /** + * #A9A9A9. + */ + static const Color kDarkGray; + + /** + * #006400. + */ + static const Color kDarkGreen; + + /** + * #BDB76B. + */ + static const Color kDarkKhaki; + + /** + * #8B008B. + */ + static const Color kDarkMagenta; + + /** + * #556B2F. + */ + static const Color kDarkOliveGreen; + + /** + * #FF8C00. + */ + static const Color kDarkOrange; + + /** + * #9932CC. + */ + static const Color kDarkOrchid; + + /** + * #8B0000. + */ + static const Color kDarkRed; + + /** + * #E9967A. + */ + static const Color kDarkSalmon; + + /** + * #8FBC8F. + */ + static const Color kDarkSeaGreen; + + /** + * #483D8B. + */ + static const Color kDarkSlateBlue; + + /** + * #2F4F4F. + */ + static const Color kDarkSlateGray; + + /** + * #00CED1. + */ + static const Color kDarkTurquoise; + + /** + * #9400D3. + */ + static const Color kDarkViolet; + + /** + * #FF1493. + */ + static const Color kDeepPink; + + /** + * #00BFFF. + */ + static const Color kDeepSkyBlue; + + /** + * #696969. + */ + static const Color kDimGray; + + /** + * #1E90FF. + */ + static const Color kDodgerBlue; + + /** + * #B22222. + */ + static const Color kFirebrick; + + /** + * #FFFAF0. + */ + static const Color kFloralWhite; + + /** + * #228B22. + */ + static const Color kForestGreen; + + /** + * #FF00FF. + */ + static const Color kFuchsia; + + /** + * #DCDCDC. + */ + static const Color kGainsboro; + + /** + * #F8F8FF. + */ + static const Color kGhostWhite; + + /** + * #FFD700. + */ + static const Color kGold; + + /** + * #DAA520. + */ + static const Color kGoldenrod; + + /** + * #808080. + */ + static const Color kGray; + + /** + * #008000. + */ + static const Color kGreen; + + /** + * #ADFF2F. + */ + static const Color kGreenYellow; + + /** + * #F0FFF0. + */ + static const Color kHoneydew; + + /** + * #FF69B4. + */ + static const Color kHotPink; + + /** + * #CD5C5C. + */ + static const Color kIndianRed; + + /** + * #4B0082. + */ + static const Color kIndigo; + + /** + * #FFFFF0. + */ + static const Color kIvory; + + /** + * #F0E68C. + */ + static const Color kKhaki; + + /** + * #E6E6FA. + */ + static const Color kLavender; + + /** + * #FFF0F5. + */ + static const Color kLavenderBlush; + + /** + * #7CFC00. + */ + static const Color kLawnGreen; + + /** + * #FFFACD. + */ + static const Color kLemonChiffon; + + /** + * #ADD8E6. + */ + static const Color kLightBlue; + + /** + * #F08080. + */ + static const Color kLightCoral; + + /** + * #E0FFFF. + */ + static const Color kLightCyan; + + /** + * #FAFAD2. + */ + static const Color kLightGoldenrodYellow; + + /** + * #D3D3D3. + */ + static const Color kLightGray; + + /** + * #90EE90. + */ + static const Color kLightGreen; + + /** + * #FFB6C1. + */ + static const Color kLightPink; + + /** + * #FFA07A. + */ + static const Color kLightSalmon; + + /** + * #20B2AA. + */ + static const Color kLightSeagGeen; + + /** + * #87CEFA. + */ + static const Color kLightSkyBlue; + + /** + * #778899. + */ + static const Color kLightSlateGray; + + /** + * #B0C4DE. + */ + static const Color kLightSteellue; + + /** + * #FFFFE0. + */ + static const Color kLightYellow; + + /** + * #00FF00. + */ + static const Color kLime; + + /** + * #32CD32. + */ + static const Color kLimeGreen; + + /** + * #FAF0E6. + */ + static const Color kLinen; + + /** + * #FF00FF. + */ + static const Color kMagenta; + + /** + * #800000. + */ + static const Color kMaroon; + + /** + * #66CDAA. + */ + static const Color kMediumAquamarine; + + /** + * #0000CD. + */ + static const Color kMediumBlue; + + /** + * #BA55D3. + */ + static const Color kMediumOrchid; + + /** + * #9370DB. + */ + static const Color kMediumPurple; + + /** + * #3CB371. + */ + static const Color kMediumSeaGreen; + + /** + * #7B68EE. + */ + static const Color kMediumSlateBlue; + + /** + * #00FA9A. + */ + static const Color kMediumSpringGreen; + + /** + * #48D1CC. + */ + static const Color kMediumTurquoise; + + /** + * #C71585. + */ + static const Color kMediumVioletRed; + + /** + * #191970. + */ + static const Color kMidnightBlue; + + /** + * #F5FFFA. + */ + static const Color kMintcream; + + /** + * #FFE4E1. + */ + static const Color kMistyRose; + + /** + * #FFE4B5. + */ + static const Color kMoccasin; + + /** + * #FFDEAD. + */ + static const Color kNavajoWhite; + + /** + * #000080. + */ + static const Color kNavy; + + /** + * #FDF5E6. + */ + static const Color kOldLace; + + /** + * #808000. + */ + static const Color kOlive; + + /** + * #6B8E23. + */ + static const Color kOliveDrab; + + /** + * #FFA500. + */ + static const Color kOrange; + + /** + * #FF4500. + */ + static const Color kOrangeRed; + + /** + * #DA70D6. + */ + static const Color kOrchid; + + /** + * #EEE8AA. + */ + static const Color kPaleGoldenrod; + + /** + * #98FB98. + */ + static const Color kPaleGreen; + + /** + * #AFEEEE. + */ + static const Color kPaleTurquoise; + + /** + * #DB7093. + */ + static const Color kPaleVioletRed; + + /** + * #FFEFD5. + */ + static const Color kPapayaWhip; + + /** + * #FFDAB9. + */ + static const Color kPeachPuff; + + /** + * #CD853F. + */ + static const Color kPeru; + + /** + * #FFC0CB. + */ + static const Color kPink; + + /** + * #DDA0DD. + */ + static const Color kPlum; + + /** + * #B0E0E6. + */ + static const Color kPowderBlue; + + /** + * #800080. + */ + static const Color kPurple; + + /** + * #FF0000. + */ + static const Color kRed; + + /** + * #BC8F8F. + */ + static const Color kRosyBrown; + + /** + * #4169E1. + */ + static const Color kRoyalBlue; + + /** + * #8B4513. + */ + static const Color kSaddleBrown; + + /** + * #FA8072. + */ + static const Color kSalmon; + + /** + * #F4A460. + */ + static const Color kSandyBrown; + + /** + * #2E8B57. + */ + static const Color kSeaGreen; + + /** + * #FFF5EE. + */ + static const Color kSeashell; + + /** + * #A0522D. + */ + static const Color kSienna; + + /** + * #C0C0C0. + */ + static const Color kSilver; + + /** + * #87CEEB. + */ + static const Color kSkyBlue; + + /** + * #6A5ACD. + */ + static const Color kSlateBlue; + + /** + * #708090. + */ + static const Color kSlateGray; + + /** + * #FFFAFA. + */ + static const Color kSnow; + + /** + * #00FF7F. + */ + static const Color kSpringGreen; + + /** + * #4682B4. + */ + static const Color kSteelBlue; + + /** + * #D2B48C. + */ + static const Color kTan; + + /** + * #008080. + */ + static const Color kTeal; + + /** + * #D8BFD8. + */ + static const Color kThistle; + + /** + * #FF6347. + */ + static const Color kTomato; + + /** + * #40E0D0. + */ + static const Color kTurquoise; + + /** + * #EE82EE. + */ + static const Color kViolet; + + /** + * #F5DEB3. + */ + static const Color kWheat; + + /** + * #FFFFFF. + */ + static const Color kWhite; + + /** + * #F5F5F5. + */ + static const Color kWhiteSmoke; + + /** + * #FFFF00. + */ + static const Color kYellow; + + /** + * #9ACD32. + */ + static const Color kYellowGreen; + + constexpr Color() = default; + + /** + * Constructs a Color. + * + * @param red Red value (0-1) + * @param green Green value (0-1) + * @param blue Blue value (0-1) + */ + constexpr Color(double r, double g, double b) + : red(roundAndClamp(r)), + green(roundAndClamp(g)), + blue(roundAndClamp(b)) {} + + double red = 0.0; + double green = 0.0; + double blue = 0.0; + + private: + static constexpr double kPrecision = 1.0 / (1 << 12); + + static constexpr double roundAndClamp(double value) { + const auto rounded = + (static_cast(value / kPrecision) + 0.5) * kPrecision; + return std::clamp(rounded, 0.0, 1.0); + } +}; + +inline bool operator==(const Color& c1, const Color& c2) { + return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue; +} + +/* + * FIRST Colors + */ +inline constexpr Color Color::kDenim{0.0823529412, 0.376470589, 0.7411764706}; +inline constexpr Color Color::kFirstBlue{0.0, 0.4, 0.7019607844}; +inline constexpr Color Color::kFirstRed{0.9294117648, 0.1098039216, + 0.1411764706}; + +/* + * Standard Colors + */ +inline constexpr Color Color::kAliceBlue{0.9411765f, 0.972549f, 1.0f}; +inline constexpr Color Color::kAntiqueWhite{0.98039216f, 0.92156863f, + 0.84313726f}; +inline constexpr Color Color::kAqua{0.0f, 1.0f, 1.0f}; +inline constexpr Color Color::kAquamarine{0.49803922f, 1.0f, 0.83137256f}; +inline constexpr Color Color::kAzure{0.9411765f, 1.0f, 1.0f}; +inline constexpr Color Color::kBeige{0.9607843f, 0.9607843f, 0.8627451f}; +inline constexpr Color Color::kBisque{1.0f, 0.89411765f, 0.76862746f}; +inline constexpr Color Color::kBlack{0.0f, 0.0f, 0.0f}; +inline constexpr Color Color::kBlanchedAlmond{1.0f, 0.92156863f, 0.8039216f}; +inline constexpr Color Color::kBlue{0.0f, 0.0f, 1.0f}; +inline constexpr Color Color::kBlueViolet{0.5411765f, 0.16862746f, 0.8862745f}; +inline constexpr Color Color::kBrown{0.64705884f, 0.16470589f, 0.16470589f}; +inline constexpr Color Color::kBurlywood{0.87058824f, 0.72156864f, 0.5294118f}; +inline constexpr Color Color::kCadetBlue{0.37254903f, 0.61960787f, 0.627451f}; +inline constexpr Color Color::kChartreuse{0.49803922f, 1.0f, 0.0f}; +inline constexpr Color Color::kChocolate{0.8235294f, 0.4117647f, 0.11764706f}; +inline constexpr Color Color::kCoral{1.0f, 0.49803922f, 0.3137255f}; +inline constexpr Color Color::kCornflowerBlue{0.39215687f, 0.58431375f, + 0.92941177f}; +inline constexpr Color Color::kCornsilk{1.0f, 0.972549f, 0.8627451f}; +inline constexpr Color Color::kCrimson{0.8627451f, 0.078431375f, 0.23529412f}; +inline constexpr Color Color::kCyan{0.0f, 1.0f, 1.0f}; +inline constexpr Color Color::kDarkBlue{0.0f, 0.0f, 0.54509807f}; +inline constexpr Color Color::kDarkCyan{0.0f, 0.54509807f, 0.54509807f}; +inline constexpr Color Color::kDarkGoldenrod{0.72156864f, 0.5254902f, + 0.043137256f}; +inline constexpr Color Color::kDarkGray{0.6627451f, 0.6627451f, 0.6627451f}; +inline constexpr Color Color::kDarkGreen{0.0f, 0.39215687f, 0.0f}; +inline constexpr Color Color::kDarkKhaki{0.7411765f, 0.7176471f, 0.41960785f}; +inline constexpr Color Color::kDarkMagenta{0.54509807f, 0.0f, 0.54509807f}; +inline constexpr Color Color::kDarkOliveGreen{0.33333334f, 0.41960785f, + 0.18431373f}; +inline constexpr Color Color::kDarkOrange{1.0f, 0.54901963f, 0.0f}; +inline constexpr Color Color::kDarkOrchid{0.6f, 0.19607843f, 0.8f}; +inline constexpr Color Color::kDarkRed{0.54509807f, 0.0f, 0.0f}; +inline constexpr Color Color::kDarkSalmon{0.9137255f, 0.5882353f, 0.47843137f}; +inline constexpr Color Color::kDarkSeaGreen{0.56078434f, 0.7372549f, + 0.56078434f}; +inline constexpr Color Color::kDarkSlateBlue{0.28235295f, 0.23921569f, + 0.54509807f}; +inline constexpr Color Color::kDarkSlateGray{0.18431373f, 0.30980393f, + 0.30980393f}; +inline constexpr Color Color::kDarkTurquoise{0.0f, 0.80784315f, 0.81960785f}; +inline constexpr Color Color::kDarkViolet{0.5803922f, 0.0f, 0.827451f}; +inline constexpr Color Color::kDeepPink{1.0f, 0.078431375f, 0.5764706f}; +inline constexpr Color Color::kDeepSkyBlue{0.0f, 0.7490196f, 1.0f}; +inline constexpr Color Color::kDimGray{0.4117647f, 0.4117647f, 0.4117647f}; +inline constexpr Color Color::kDodgerBlue{0.11764706f, 0.5647059f, 1.0f}; +inline constexpr Color Color::kFirebrick{0.69803923f, 0.13333334f, 0.13333334f}; +inline constexpr Color Color::kFloralWhite{1.0f, 0.98039216f, 0.9411765f}; +inline constexpr Color Color::kForestGreen{0.13333334f, 0.54509807f, + 0.13333334f}; +inline constexpr Color Color::kFuchsia{1.0f, 0.0f, 1.0f}; +inline constexpr Color Color::kGainsboro{0.8627451f, 0.8627451f, 0.8627451f}; +inline constexpr Color Color::kGhostWhite{0.972549f, 0.972549f, 1.0f}; +inline constexpr Color Color::kGold{1.0f, 0.84313726f, 0.0f}; +inline constexpr Color Color::kGoldenrod{0.85490197f, 0.64705884f, 0.1254902f}; +inline constexpr Color Color::kGray{0.5019608f, 0.5019608f, 0.5019608f}; +inline constexpr Color Color::kGreen{0.0f, 0.5019608f, 0.0f}; +inline constexpr Color Color::kGreenYellow{0.6784314f, 1.0f, 0.18431373f}; +inline constexpr Color Color::kHoneydew{0.9411765f, 1.0f, 0.9411765f}; +inline constexpr Color Color::kHotPink{1.0f, 0.4117647f, 0.7058824f}; +inline constexpr Color Color::kIndianRed{0.8039216f, 0.36078432f, 0.36078432f}; +inline constexpr Color Color::kIndigo{0.29411766f, 0.0f, 0.50980395f}; +inline constexpr Color Color::kIvory{1.0f, 1.0f, 0.9411765f}; +inline constexpr Color Color::kKhaki{0.9411765f, 0.9019608f, 0.54901963f}; +inline constexpr Color Color::kLavender{0.9019608f, 0.9019608f, 0.98039216f}; +inline constexpr Color Color::kLavenderBlush{1.0f, 0.9411765f, 0.9607843f}; +inline constexpr Color Color::kLawnGreen{0.4862745f, 0.9882353f, 0.0f}; +inline constexpr Color Color::kLemonChiffon{1.0f, 0.98039216f, 0.8039216f}; +inline constexpr Color Color::kLightBlue{0.6784314f, 0.84705883f, 0.9019608f}; +inline constexpr Color Color::kLightCoral{0.9411765f, 0.5019608f, 0.5019608f}; +inline constexpr Color Color::kLightCyan{0.8784314f, 1.0f, 1.0f}; +inline constexpr Color Color::kLightGoldenrodYellow{0.98039216f, 0.98039216f, + 0.8235294f}; +inline constexpr Color Color::kLightGray{0.827451f, 0.827451f, 0.827451f}; +inline constexpr Color Color::kLightGreen{0.5647059f, 0.93333334f, 0.5647059f}; +inline constexpr Color Color::kLightPink{1.0f, 0.7137255f, 0.75686276f}; +inline constexpr Color Color::kLightSalmon{1.0f, 0.627451f, 0.47843137f}; +inline constexpr Color Color::kLightSeagGeen{0.1254902f, 0.69803923f, + 0.6666667f}; +inline constexpr Color Color::kLightSkyBlue{0.5294118f, 0.80784315f, + 0.98039216f}; +inline constexpr Color Color::kLightSlateGray{0.46666667f, 0.53333336f, 0.6f}; +inline constexpr Color Color::kLightSteellue{0.6901961f, 0.76862746f, + 0.87058824f}; +inline constexpr Color Color::kLightYellow{1.0f, 1.0f, 0.8784314f}; +inline constexpr Color Color::kLime{0.0f, 1.0f, 0.0f}; +inline constexpr Color Color::kLimeGreen{0.19607843f, 0.8039216f, 0.19607843f}; +inline constexpr Color Color::kLinen{0.98039216f, 0.9411765f, 0.9019608f}; +inline constexpr Color Color::kMagenta{1.0f, 0.0f, 1.0f}; +inline constexpr Color Color::kMaroon{0.5019608f, 0.0f, 0.0f}; +inline constexpr Color Color::kMediumAquamarine{0.4f, 0.8039216f, 0.6666667f}; +inline constexpr Color Color::kMediumBlue{0.0f, 0.0f, 0.8039216f}; +inline constexpr Color Color::kMediumOrchid{0.7294118f, 0.33333334f, 0.827451f}; +inline constexpr Color Color::kMediumPurple{0.5764706f, 0.4392157f, + 0.85882354f}; +inline constexpr Color Color::kMediumSeaGreen{0.23529412f, 0.7019608f, + 0.44313726f}; +inline constexpr Color Color::kMediumSlateBlue{0.48235294f, 0.40784314f, + 0.93333334f}; +inline constexpr Color Color::kMediumSpringGreen{0.0f, 0.98039216f, 0.6039216f}; +inline constexpr Color Color::kMediumTurquoise{0.28235295f, 0.81960785f, 0.8f}; +inline constexpr Color Color::kMediumVioletRed{0.78039217f, 0.08235294f, + 0.52156866f}; +inline constexpr Color Color::kMidnightBlue{0.09803922f, 0.09803922f, + 0.4392157f}; +inline constexpr Color Color::kMintcream{0.9607843f, 1.0f, 0.98039216f}; +inline constexpr Color Color::kMistyRose{1.0f, 0.89411765f, 0.88235295f}; +inline constexpr Color Color::kMoccasin{1.0f, 0.89411765f, 0.70980394f}; +inline constexpr Color Color::kNavajoWhite{1.0f, 0.87058824f, 0.6784314f}; +inline constexpr Color Color::kNavy{0.0f, 0.0f, 0.5019608f}; +inline constexpr Color Color::kOldLace{0.99215686f, 0.9607843f, 0.9019608f}; +inline constexpr Color Color::kOlive{0.5019608f, 0.5019608f, 0.0f}; +inline constexpr Color Color::kOliveDrab{0.41960785f, 0.5568628f, 0.13725491f}; +inline constexpr Color Color::kOrange{1.0f, 0.64705884f, 0.0f}; +inline constexpr Color Color::kOrangeRed{1.0f, 0.27058825f, 0.0f}; +inline constexpr Color Color::kOrchid{0.85490197f, 0.4392157f, 0.8392157f}; +inline constexpr Color Color::kPaleGoldenrod{0.93333334f, 0.9098039f, + 0.6666667f}; +inline constexpr Color Color::kPaleGreen{0.59607846f, 0.9843137f, 0.59607846f}; +inline constexpr Color Color::kPaleTurquoise{0.6862745f, 0.93333334f, + 0.93333334f}; +inline constexpr Color Color::kPaleVioletRed{0.85882354f, 0.4392157f, + 0.5764706f}; +inline constexpr Color Color::kPapayaWhip{1.0f, 0.9372549f, 0.8352941f}; +inline constexpr Color Color::kPeachPuff{1.0f, 0.85490197f, 0.7254902f}; +inline constexpr Color Color::kPeru{0.8039216f, 0.52156866f, 0.24705882f}; +inline constexpr Color Color::kPink{1.0f, 0.7529412f, 0.79607844f}; +inline constexpr Color Color::kPlum{0.8666667f, 0.627451f, 0.8666667f}; +inline constexpr Color Color::kPowderBlue{0.6901961f, 0.8784314f, 0.9019608f}; +inline constexpr Color Color::kPurple{0.5019608f, 0.0f, 0.5019608f}; +inline constexpr Color Color::kRed{1.0f, 0.0f, 0.0f}; +inline constexpr Color Color::kRosyBrown{0.7372549f, 0.56078434f, 0.56078434f}; +inline constexpr Color Color::kRoyalBlue{0.25490198f, 0.4117647f, 0.88235295f}; +inline constexpr Color Color::kSaddleBrown{0.54509807f, 0.27058825f, + 0.07450981f}; +inline constexpr Color Color::kSalmon{0.98039216f, 0.5019608f, 0.44705883f}; +inline constexpr Color Color::kSandyBrown{0.95686275f, 0.6431373f, 0.3764706f}; +inline constexpr Color Color::kSeaGreen{0.18039216f, 0.54509807f, 0.34117648f}; +inline constexpr Color Color::kSeashell{1.0f, 0.9607843f, 0.93333334f}; +inline constexpr Color Color::kSienna{0.627451f, 0.32156864f, 0.1764706f}; +inline constexpr Color Color::kSilver{0.7529412f, 0.7529412f, 0.7529412f}; +inline constexpr Color Color::kSkyBlue{0.5294118f, 0.80784315f, 0.92156863f}; +inline constexpr Color Color::kSlateBlue{0.41568628f, 0.3529412f, 0.8039216f}; +inline constexpr Color Color::kSlateGray{0.4392157f, 0.5019608f, 0.5647059f}; +inline constexpr Color Color::kSnow{1.0f, 0.98039216f, 0.98039216f}; +inline constexpr Color Color::kSpringGreen{0.0f, 1.0f, 0.49803922f}; +inline constexpr Color Color::kSteelBlue{0.27450982f, 0.50980395f, 0.7058824f}; +inline constexpr Color Color::kTan{0.8235294f, 0.7058824f, 0.54901963f}; +inline constexpr Color Color::kTeal{0.0f, 0.5019608f, 0.5019608f}; +inline constexpr Color Color::kThistle{0.84705883f, 0.7490196f, 0.84705883f}; +inline constexpr Color Color::kTomato{1.0f, 0.3882353f, 0.2784314f}; +inline constexpr Color Color::kTurquoise{0.2509804f, 0.8784314f, 0.8156863f}; +inline constexpr Color Color::kViolet{0.93333334f, 0.50980395f, 0.93333334f}; +inline constexpr Color Color::kWheat{0.9607843f, 0.87058824f, 0.7019608f}; +inline constexpr Color Color::kWhite{1.0f, 1.0f, 1.0f}; +inline constexpr Color Color::kWhiteSmoke{0.9607843f, 0.9607843f, 0.9607843f}; +inline constexpr Color Color::kYellow{1.0f, 1.0f, 0.0f}; +inline constexpr Color Color::kYellowGreen{0.6039216f, 0.8039216f, 0.19607843f}; + +} // namespace frc diff --git a/wpilibc/src/main/native/include/frc/util/Color8Bit.h b/wpilibc/src/main/native/include/frc/util/Color8Bit.h new file mode 100644 index 0000000000..d5dba078de --- /dev/null +++ b/wpilibc/src/main/native/include/frc/util/Color8Bit.h @@ -0,0 +1,58 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include "Color.h" + +namespace frc { + +/** + * Represents colors that can be used with Addressable LEDs. + */ +class Color8Bit { + public: + constexpr Color8Bit() = default; + + /** + * Constructs a Color8Bit. + * + * @param red Red value (0-255) + * @param green Green value (0-255) + * @param blue Blue value (0-255) + */ + constexpr Color8Bit(int r, int g, int b) + : red(std::clamp(r, 0, 255)), + green(std::clamp(g, 0, 255)), + blue(std::clamp(b, 0, 255)) {} + + /** + * Constructs a Color8Bit from a Color. + * + * @param color The color + */ + constexpr Color8Bit(const Color& color) + : red(color.red * 255), + green(color.green * 255), + blue(color.blue * 255) {} + + constexpr operator Color() const { + return Color(red / 255.0, green / 255.0, blue / 255.0); + } + + int red = 0; + int green = 0; + int blue = 0; +}; + +inline bool operator==(const Color8Bit& c1, const Color8Bit& c2) { + return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue; +} + +} // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java index 8f9c1b4f12..3cc0763373 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLEDBuffer.java @@ -7,6 +7,9 @@ package edu.wpi.first.wpilibj; +import edu.wpi.first.wpilibj.util.Color; +import edu.wpi.first.wpilibj.util.Color8Bit; + /** * Buffer storage for Addressable LEDs. */ @@ -26,9 +29,9 @@ public class AddressableLEDBuffer { * Sets a specific led in the buffer. * * @param index the index to write - * @param r the r value [0-255] - * @param g the g value [0-255] - * @param b the b value [0-255] + * @param r the r value [0-255] + * @param g the g value [0-255] + * @param b the b value [0-255] */ @SuppressWarnings("ParameterName") public void setRGB(int index, int r, int g, int b) { @@ -42,9 +45,9 @@ public class AddressableLEDBuffer { * Sets a specific led in the buffer. * * @param index the index to write - * @param h the h value [0-180] - * @param s the s value [0-255] - * @param v the v value [0-255] + * @param h the h value [0-180] + * @param s the s value [0-255] + * @param v the v value [0-255] */ @SuppressWarnings("ParameterName") public void setHSV(final int index, final int h, final int s, final int v) { @@ -82,6 +85,29 @@ public class AddressableLEDBuffer { } } + /** + * Sets a specific LED in the buffer. + * + * @param index The index to write + * @param color The color of the LED + */ + public void setLED(int index, Color color) { + setRGB(index, + (int) (color.red * 255), + (int) (color.green * 255), + (int) (color.blue * 255)); + } + + /** + * Sets a specific LED in the buffer. + * + * @param index The index to write + * @param color The color of the LED + */ + public void setLED(int index, Color8Bit color) { + setRGB(index, color.red, color.green, color.blue); + } + /** * Gets the buffer length. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java new file mode 100644 index 0000000000..e4d5b697f2 --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java @@ -0,0 +1,798 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.util; + +import java.util.Objects; + +import edu.wpi.first.wpiutil.math.MathUtil; + +/** + * Represents colors. + * + *

Limited to 12 bits of precision. + */ +@SuppressWarnings("MemberName") +public class Color { + /* + * FIRST Colors + */ + + /** + * #1560BD. + */ + public static final Color kDenim = new Color(0.0823529412, 0.376470589, 0.7411764706); + + /** + * #0066B3. + */ + public static final Color kFirstBlue = new Color(0.0, 0.4, 0.7019607844); + + /** + * #ED1C24. + */ + public static final Color kFirstRed = new Color(0.9294117648, 0.1098039216, 0.1411764706); + + /* + * Standard Colors + */ + + /** + * #F0F8FF. + */ + public static final Color kAliceBlue = new Color(0.9411765f, 0.972549f, 1.0f); + + /** + * #FAEBD7. + */ + public static final Color kAntiqueWhite = new Color(0.98039216f, 0.92156863f, 0.84313726f); + + /** + * #00FFFF. + */ + public static final Color kAqua = new Color(0.0f, 1.0f, 1.0f); + + /** + * #7FFFD4. + */ + public static final Color kAquamarine = new Color(0.49803922f, 1.0f, 0.83137256f); + + /** + * #F0FFFF. + */ + public static final Color kAzure = new Color(0.9411765f, 1.0f, 1.0f); + + /** + * #F5F5DC. + */ + public static final Color kBeige = new Color(0.9607843f, 0.9607843f, 0.8627451f); + + /** + * #FFE4C4. + */ + public static final Color kBisque = new Color(1.0f, 0.89411765f, 0.76862746f); + + /** + * #000000. + */ + public static final Color kBlack = new Color(0.0f, 0.0f, 0.0f); + + /** + * #FFEBCD. + */ + public static final Color kBlanchedAlmond = new Color(1.0f, 0.92156863f, 0.8039216f); + + /** + * #0000FF. + */ + public static final Color kBlue = new Color(0.0f, 0.0f, 1.0f); + + /** + * #8A2BE2. + */ + public static final Color kBlueViolet = new Color(0.5411765f, 0.16862746f, 0.8862745f); + + /** + * #A52A2A. + */ + public static final Color kBrown = new Color(0.64705884f, 0.16470589f, 0.16470589f); + + /** + * #DEB887. + */ + public static final Color kBurlywood = new Color(0.87058824f, 0.72156864f, 0.5294118f); + + /** + * #5F9EA0. + */ + public static final Color kCadetBlue = new Color(0.37254903f, 0.61960787f, 0.627451f); + + /** + * #7FFF00. + */ + public static final Color kChartreuse = new Color(0.49803922f, 1.0f, 0.0f); + + /** + * #D2691E. + */ + public static final Color kChocolate = new Color(0.8235294f, 0.4117647f, 0.11764706f); + + /** + * #FF7F50. + */ + public static final Color kCoral = new Color(1.0f, 0.49803922f, 0.3137255f); + + /** + * #6495ED. + */ + public static final Color kCornflowerBlue = new Color(0.39215687f, 0.58431375f, 0.92941177f); + + /** + * #FFF8DC. + */ + public static final Color kCornsilk = new Color(1.0f, 0.972549f, 0.8627451f); + + /** + * #DC143C. + */ + public static final Color kCrimson = new Color(0.8627451f, 0.078431375f, 0.23529412f); + + /** + * #00FFFF. + */ + public static final Color kCyan = new Color(0.0f, 1.0f, 1.0f); + + /** + * #00008B. + */ + public static final Color kDarkBlue = new Color(0.0f, 0.0f, 0.54509807f); + + /** + * #008B8B. + */ + public static final Color kDarkCyan = new Color(0.0f, 0.54509807f, 0.54509807f); + + /** + * #B8860B. + */ + public static final Color kDarkGoldenrod = new Color(0.72156864f, 0.5254902f, 0.043137256f); + + /** + * #A9A9A9. + */ + public static final Color kDarkGray = new Color(0.6627451f, 0.6627451f, 0.6627451f); + + /** + * #006400. + */ + public static final Color kDarkGreen = new Color(0.0f, 0.39215687f, 0.0f); + + /** + * #BDB76B. + */ + public static final Color kDarkKhaki = new Color(0.7411765f, 0.7176471f, 0.41960785f); + + /** + * #8B008B. + */ + public static final Color kDarkMagenta = new Color(0.54509807f, 0.0f, 0.54509807f); + + /** + * #556B2F. + */ + public static final Color kDarkOliveGreen = new Color(0.33333334f, 0.41960785f, 0.18431373f); + + /** + * #FF8C00. + */ + public static final Color kDarkOrange = new Color(1.0f, 0.54901963f, 0.0f); + + /** + * #9932CC. + */ + public static final Color kDarkOrchid = new Color(0.6f, 0.19607843f, 0.8f); + + /** + * #8B0000. + */ + public static final Color kDarkRed = new Color(0.54509807f, 0.0f, 0.0f); + + /** + * #E9967A. + */ + public static final Color kDarkSalmon = new Color(0.9137255f, 0.5882353f, 0.47843137f); + + /** + * #8FBC8F. + */ + public static final Color kDarkSeaGreen = new Color(0.56078434f, 0.7372549f, 0.56078434f); + + /** + * #483D8B. + */ + public static final Color kDarkSlateBlue = new Color(0.28235295f, 0.23921569f, 0.54509807f); + + /** + * #2F4F4F. + */ + public static final Color kDarkSlateGray = new Color(0.18431373f, 0.30980393f, 0.30980393f); + + /** + * #00CED1. + */ + public static final Color kDarkTurquoise = new Color(0.0f, 0.80784315f, 0.81960785f); + + /** + * #9400D3. + */ + public static final Color kDarkViolet = new Color(0.5803922f, 0.0f, 0.827451f); + + /** + * #FF1493. + */ + public static final Color kDeepPink = new Color(1.0f, 0.078431375f, 0.5764706f); + + /** + * #00BFFF. + */ + public static final Color kDeepSkyBlue = new Color(0.0f, 0.7490196f, 1.0f); + + /** + * #696969. + */ + public static final Color kDimGray = new Color(0.4117647f, 0.4117647f, 0.4117647f); + + /** + * #1E90FF. + */ + public static final Color kDodgerBlue = new Color(0.11764706f, 0.5647059f, 1.0f); + + /** + * #B22222. + */ + public static final Color kFirebrick = new Color(0.69803923f, 0.13333334f, 0.13333334f); + + /** + * #FFFAF0. + */ + public static final Color kFloralWhite = new Color(1.0f, 0.98039216f, 0.9411765f); + + /** + * #228B22. + */ + public static final Color kForestGreen = new Color(0.13333334f, 0.54509807f, 0.13333334f); + + /** + * #FF00FF. + */ + public static final Color kFuchsia = new Color(1.0f, 0.0f, 1.0f); + + /** + * #DCDCDC. + */ + public static final Color kGainsboro = new Color(0.8627451f, 0.8627451f, 0.8627451f); + + /** + * #F8F8FF. + */ + public static final Color kGhostWhite = new Color(0.972549f, 0.972549f, 1.0f); + + /** + * #FFD700. + */ + public static final Color kGold = new Color(1.0f, 0.84313726f, 0.0f); + + /** + * #DAA520. + */ + public static final Color kGoldenrod = new Color(0.85490197f, 0.64705884f, 0.1254902f); + + /** + * #808080. + */ + public static final Color kGray = new Color(0.5019608f, 0.5019608f, 0.5019608f); + + /** + * #008000. + */ + public static final Color kGreen = new Color(0.0f, 0.5019608f, 0.0f); + + /** + * #ADFF2F. + */ + public static final Color kGreenYellow = new Color(0.6784314f, 1.0f, 0.18431373f); + + /** + * #F0FFF0. + */ + public static final Color kHoneydew = new Color(0.9411765f, 1.0f, 0.9411765f); + + /** + * #FF69B4. + */ + public static final Color kHotPink = new Color(1.0f, 0.4117647f, 0.7058824f); + + /** + * #CD5C5C. + */ + public static final Color kIndianRed = new Color(0.8039216f, 0.36078432f, 0.36078432f); + + /** + * #4B0082. + */ + public static final Color kIndigo = new Color(0.29411766f, 0.0f, 0.50980395f); + + /** + * #FFFFF0. + */ + public static final Color kIvory = new Color(1.0f, 1.0f, 0.9411765f); + + /** + * #F0E68C. + */ + public static final Color kKhaki = new Color(0.9411765f, 0.9019608f, 0.54901963f); + + /** + * #E6E6FA. + */ + public static final Color kLavender = new Color(0.9019608f, 0.9019608f, 0.98039216f); + + /** + * #FFF0F5. + */ + public static final Color kLavenderBlush = new Color(1.0f, 0.9411765f, 0.9607843f); + + /** + * #7CFC00. + */ + public static final Color kLawnGreen = new Color(0.4862745f, 0.9882353f, 0.0f); + + /** + * #FFFACD. + */ + public static final Color kLemonChiffon = new Color(1.0f, 0.98039216f, 0.8039216f); + + /** + * #ADD8E6. + */ + public static final Color kLightBlue = new Color(0.6784314f, 0.84705883f, 0.9019608f); + + /** + * #F08080. + */ + public static final Color kLightCoral = new Color(0.9411765f, 0.5019608f, 0.5019608f); + + /** + * #E0FFFF. + */ + public static final Color kLightCyan = new Color(0.8784314f, 1.0f, 1.0f); + + /** + * #FAFAD2. + */ + public static final Color kLightGoldenrodYellow = new Color(0.98039216f, 0.98039216f, 0.8235294f); + + /** + * #D3D3D3. + */ + public static final Color kLightGray = new Color(0.827451f, 0.827451f, 0.827451f); + + /** + * #90EE90. + */ + public static final Color kLightGreen = new Color(0.5647059f, 0.93333334f, 0.5647059f); + + /** + * #FFB6C1. + */ + public static final Color kLightPink = new Color(1.0f, 0.7137255f, 0.75686276f); + + /** + * #FFA07A. + */ + public static final Color kLightSalmon = new Color(1.0f, 0.627451f, 0.47843137f); + + /** + * #20B2AA. + */ + public static final Color kLightSeagGeen = new Color(0.1254902f, 0.69803923f, 0.6666667f); + + /** + * #87CEFA. + */ + public static final Color kLightSkyBlue = new Color(0.5294118f, 0.80784315f, 0.98039216f); + + /** + * #778899. + */ + public static final Color kLightSlateGray = new Color(0.46666667f, 0.53333336f, 0.6f); + + /** + * #B0C4DE. + */ + public static final Color kLightSteellue = new Color(0.6901961f, 0.76862746f, 0.87058824f); + + /** + * #FFFFE0. + */ + public static final Color kLightYellow = new Color(1.0f, 1.0f, 0.8784314f); + + /** + * #00FF00. + */ + public static final Color kLime = new Color(0.0f, 1.0f, 0.0f); + + /** + * #32CD32. + */ + public static final Color kLimeGreen = new Color(0.19607843f, 0.8039216f, 0.19607843f); + + /** + * #FAF0E6. + */ + public static final Color kLinen = new Color(0.98039216f, 0.9411765f, 0.9019608f); + + /** + * #FF00FF. + */ + public static final Color kMagenta = new Color(1.0f, 0.0f, 1.0f); + + /** + * #800000. + */ + public static final Color kMaroon = new Color(0.5019608f, 0.0f, 0.0f); + + /** + * #66CDAA. + */ + public static final Color kMediumAquamarine = new Color(0.4f, 0.8039216f, 0.6666667f); + + /** + * #0000CD. + */ + public static final Color kMediumBlue = new Color(0.0f, 0.0f, 0.8039216f); + + /** + * #BA55D3. + */ + public static final Color kMediumOrchid = new Color(0.7294118f, 0.33333334f, 0.827451f); + + /** + * #9370DB. + */ + public static final Color kMediumPurple = new Color(0.5764706f, 0.4392157f, 0.85882354f); + + /** + * #3CB371. + */ + public static final Color kMediumSeaGreen = new Color(0.23529412f, 0.7019608f, 0.44313726f); + + /** + * #7B68EE. + */ + public static final Color kMediumSlateBlue = new Color(0.48235294f, 0.40784314f, 0.93333334f); + + /** + * #00FA9A. + */ + public static final Color kMediumSpringGreen = new Color(0.0f, 0.98039216f, 0.6039216f); + + /** + * #48D1CC. + */ + public static final Color kMediumTurquoise = new Color(0.28235295f, 0.81960785f, 0.8f); + + /** + * #C71585. + */ + public static final Color kMediumVioletRed = new Color(0.78039217f, 0.08235294f, 0.52156866f); + + /** + * #191970. + */ + public static final Color kMidnightBlue = new Color(0.09803922f, 0.09803922f, 0.4392157f); + + /** + * #F5FFFA. + */ + public static final Color kMintcream = new Color(0.9607843f, 1.0f, 0.98039216f); + + /** + * #FFE4E1. + */ + public static final Color kMistyRose = new Color(1.0f, 0.89411765f, 0.88235295f); + + /** + * #FFE4B5. + */ + public static final Color kMoccasin = new Color(1.0f, 0.89411765f, 0.70980394f); + + /** + * #FFDEAD. + */ + public static final Color kNavajoWhite = new Color(1.0f, 0.87058824f, 0.6784314f); + + /** + * #000080. + */ + public static final Color kNavy = new Color(0.0f, 0.0f, 0.5019608f); + + /** + * #FDF5E6. + */ + public static final Color kOldLace = new Color(0.99215686f, 0.9607843f, 0.9019608f); + + /** + * #808000. + */ + public static final Color kOlive = new Color(0.5019608f, 0.5019608f, 0.0f); + + /** + * #6B8E23. + */ + public static final Color kOliveDrab = new Color(0.41960785f, 0.5568628f, 0.13725491f); + + /** + * #FFA500. + */ + public static final Color kOrange = new Color(1.0f, 0.64705884f, 0.0f); + + /** + * #FF4500. + */ + public static final Color kOrangeRed = new Color(1.0f, 0.27058825f, 0.0f); + + /** + * #DA70D6. + */ + public static final Color kOrchid = new Color(0.85490197f, 0.4392157f, 0.8392157f); + + /** + * #EEE8AA. + */ + public static final Color kPaleGoldenrod = new Color(0.93333334f, 0.9098039f, 0.6666667f); + + /** + * #98FB98. + */ + public static final Color kPaleGreen = new Color(0.59607846f, 0.9843137f, 0.59607846f); + + /** + * #AFEEEE. + */ + public static final Color kPaleTurquoise = new Color(0.6862745f, 0.93333334f, 0.93333334f); + + /** + * #DB7093. + */ + public static final Color kPaleVioletRed = new Color(0.85882354f, 0.4392157f, 0.5764706f); + + /** + * #FFEFD5. + */ + public static final Color kPapayaWhip = new Color(1.0f, 0.9372549f, 0.8352941f); + + /** + * #FFDAB9. + */ + public static final Color kPeachPuff = new Color(1.0f, 0.85490197f, 0.7254902f); + + /** + * #CD853F. + */ + public static final Color kPeru = new Color(0.8039216f, 0.52156866f, 0.24705882f); + + /** + * #FFC0CB. + */ + public static final Color kPink = new Color(1.0f, 0.7529412f, 0.79607844f); + + /** + * #DDA0DD. + */ + public static final Color kPlum = new Color(0.8666667f, 0.627451f, 0.8666667f); + + /** + * #B0E0E6. + */ + public static final Color kPowderBlue = new Color(0.6901961f, 0.8784314f, 0.9019608f); + + /** + * #800080. + */ + public static final Color kPurple = new Color(0.5019608f, 0.0f, 0.5019608f); + + /** + * #FF0000. + */ + public static final Color kRed = new Color(1.0f, 0.0f, 0.0f); + + /** + * #BC8F8F. + */ + public static final Color kRosyBrown = new Color(0.7372549f, 0.56078434f, 0.56078434f); + + /** + * #4169E1. + */ + public static final Color kRoyalBlue = new Color(0.25490198f, 0.4117647f, 0.88235295f); + + /** + * #8B4513. + */ + public static final Color kSaddleBrown = new Color(0.54509807f, 0.27058825f, 0.07450981f); + + /** + * #FA8072. + */ + public static final Color kSalmon = new Color(0.98039216f, 0.5019608f, 0.44705883f); + + /** + * #F4A460. + */ + public static final Color kSandyBrown = new Color(0.95686275f, 0.6431373f, 0.3764706f); + + /** + * #2E8B57. + */ + public static final Color kSeaGreen = new Color(0.18039216f, 0.54509807f, 0.34117648f); + + /** + * #FFF5EE. + */ + public static final Color kSeashell = new Color(1.0f, 0.9607843f, 0.93333334f); + + /** + * #A0522D. + */ + public static final Color kSienna = new Color(0.627451f, 0.32156864f, 0.1764706f); + + /** + * #C0C0C0. + */ + public static final Color kSilver = new Color(0.7529412f, 0.7529412f, 0.7529412f); + + /** + * #87CEEB. + */ + public static final Color kSkyBlue = new Color(0.5294118f, 0.80784315f, 0.92156863f); + + /** + * #6A5ACD. + */ + public static final Color kSlateBlue = new Color(0.41568628f, 0.3529412f, 0.8039216f); + + /** + * #708090. + */ + public static final Color kSlateGray = new Color(0.4392157f, 0.5019608f, 0.5647059f); + + /** + * #FFFAFA. + */ + public static final Color kSnow = new Color(1.0f, 0.98039216f, 0.98039216f); + + /** + * #00FF7F. + */ + public static final Color kSpringGreen = new Color(0.0f, 1.0f, 0.49803922f); + + /** + * #4682B4. + */ + public static final Color kSteelBlue = new Color(0.27450982f, 0.50980395f, 0.7058824f); + + /** + * #D2B48C. + */ + public static final Color kTan = new Color(0.8235294f, 0.7058824f, 0.54901963f); + + /** + * #008080. + */ + public static final Color kTeal = new Color(0.0f, 0.5019608f, 0.5019608f); + + /** + * #D8BFD8. + */ + public static final Color kThistle = new Color(0.84705883f, 0.7490196f, 0.84705883f); + + /** + * #FF6347. + */ + public static final Color kTomato = new Color(1.0f, 0.3882353f, 0.2784314f); + + /** + * #40E0D0. + */ + public static final Color kTurquoise = new Color(0.2509804f, 0.8784314f, 0.8156863f); + + /** + * #EE82EE. + */ + public static final Color kViolet = new Color(0.93333334f, 0.50980395f, 0.93333334f); + + /** + * #F5DEB3. + */ + public static final Color kWheat = new Color(0.9607843f, 0.87058824f, 0.7019608f); + + /** + * #FFFFFF. + */ + public static final Color kWhite = new Color(1.0f, 1.0f, 1.0f); + + /** + * #F5F5F5. + */ + public static final Color kWhiteSmoke = new Color(0.9607843f, 0.9607843f, 0.9607843f); + + /** + * #FFFF00. + */ + public static final Color kYellow = new Color(1.0f, 1.0f, 0.0f); + + /** + * #9ACD32. + */ + public static final Color kYellowGreen = new Color(0.6039216f, 0.8039216f, 0.19607843f); + + public final double red; + public final double green; + public final double blue; + + private static final double kPrecision = Math.pow(2, -12); + + /** + * Constructs a Color. + * + * @param red Red value (0-1) + * @param green Green value (0-1) + * @param blue Blue value (0-1) + */ + Color(double red, double green, double blue) { + this.red = roundAndClamp(red); + this.green = roundAndClamp(green); + this.blue = roundAndClamp(blue); + } + + /** + * Constructs a Color from a Color8Bit. + * + * @param color The color + */ + public Color(Color8Bit color) { + this(color.red / 255.0, + color.green / 255.0, + color.blue / 255.0); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + + Color color = (Color) other; + return Double.compare(color.red, red) == 0 + && Double.compare(color.green, green) == 0 + && Double.compare(color.blue, blue) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(red, green, blue); + } + + private static double roundAndClamp(double value) { + final var rounded = Math.round(value / kPrecision) * kPrecision; + return MathUtil.clamp(rounded, 0.0, 1.0); + } +} diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java new file mode 100644 index 0000000000..50d42fc42e --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java @@ -0,0 +1,66 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.util; + +import java.util.Objects; + +import edu.wpi.first.wpiutil.math.MathUtil; + +/** + * Represents colors with 8 bits of precision. + */ +@SuppressWarnings("MemberName") +public class Color8Bit { + public final int red; + public final int green; + public final int blue; + + /** + * Constructs a Color8Bit. + * + * @param red Red value (0-255) + * @param green Green value (0-255) + * @param blue Blue value (0-255) + */ + public Color8Bit(int red, int green, int blue) { + this.red = MathUtil.clamp(red, 0, 255); + this.green = MathUtil.clamp(green, 0, 255); + this.blue = MathUtil.clamp(blue, 0, 255); + } + + /** + * Constructs a Color8Bit from a Color. + * + * @param color The color + */ + public Color8Bit(Color color) { + this((int) (color.red * 255), + (int) (color.green * 255), + (int) (color.blue * 255)); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + + Color8Bit color8Bit = (Color8Bit) other; + return red == color8Bit.red + && green == color8Bit.green + && blue == color8Bit.blue; + } + + @Override + public int hashCode() { + return Objects.hash(red, green, blue); + } +} diff --git a/wpiutil/src/main/java/edu/wpi/first/wpiutil/math/MathUtil.java b/wpiutil/src/main/java/edu/wpi/first/wpiutil/math/MathUtil.java index cf578349fb..bd555dc770 100644 --- a/wpiutil/src/main/java/edu/wpi/first/wpiutil/math/MathUtil.java +++ b/wpiutil/src/main/java/edu/wpi/first/wpiutil/math/MathUtil.java @@ -12,6 +12,17 @@ public final class MathUtil { throw new AssertionError("utility class"); } + /** + * Returns value clamped between low and high boundaries. + * + * @param value Value to clamp. + * @param low The lower boundary to which to clamp value. + * @param high The higher boundary to which to clamp value. + */ + public static int clamp(int value, int low, int high) { + return Math.max(low, Math.min(value, high)); + } + /** * Returns value clamped between low and high boundaries. *