RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Color8Bit.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.util;
6
7import java.util.Objects;
8
10public class Color8Bit {
12 public final int red;
13
15 public final int green;
16
18 public final int blue;
19
21 public Color8Bit() {
22 red = 0;
23 green = 0;
24 blue = 0;
25 }
26
34 public Color8Bit(int red, int green, int blue) {
35 this.red = Math.clamp(red, 0, 255);
36 this.green = Math.clamp(green, 0, 255);
37 this.blue = Math.clamp(blue, 0, 255);
38 }
39
45 public Color8Bit(Color color) {
46 this((int) (color.red * 255), (int) (color.green * 255), (int) (color.blue * 255));
47 }
48
55 public Color8Bit(String hexString) {
56 if (hexString.length() != 7 || !hexString.startsWith("#")) {
57 throw new IllegalArgumentException("Invalid hex string \"" + hexString + "\"");
58 }
59
60 this.red = Integer.valueOf(hexString.substring(1, 3), 16);
61 this.green = Integer.valueOf(hexString.substring(3, 5), 16);
62 this.blue = Integer.valueOf(hexString.substring(5, 7), 16);
63 }
64
65 @Override
66 public boolean equals(Object other) {
67 if (this == other) {
68 return true;
69 }
70 if (other == null) {
71 return false;
72 }
73
74 return other instanceof Color8Bit color8Bit
75 && red == color8Bit.red
76 && green == color8Bit.green
77 && blue == color8Bit.blue;
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(red, green, blue);
83 }
84
85 @Override
86 public String toString() {
87 return toHexString();
88 }
89
95 public String toHexString() {
96 return String.format("#%02X%02X%02X", red, green, blue);
97 }
98}
Color8Bit(String hexString)
Color8Bit(int red, int green, int blue)