枚举存储RGB字符串 [英] enum to store RGB strings
问题描述
public enum Colors {
GRAY(142,142,147),
RED(255,59,48),
GREEN(76,217,100),
PURPLE(88,86,214),
LIGHTBLUE 52,170,220); // ...等等,这是一个简短的列表
private Colors(final String string){
this.string = string;
}
private final String string;
public String getRGB(){
return string;
}
}
public class HelloWorld {
public static void main(String [] args){
String test = Colors。 LIGHTBLUE.getRGB();
System.out.println(test);
}
}
可能将其更改为以下内容:
public enum Colors {
GRAY(142,142,147) ,
RED(255,59,48),
GREEN(76,217,100),
PURPLE(88,86,214),
LIGHTBLUE(52,170, 220); // ...等等,这是一个简短的列表
private final int r;
private final int g;
private final int b;
private final String rgb;
private Colors(final int r,final int g,final int b){
this.r = r;
this.g = g;
this.b = b;
this.rgb = r +,+ g +,+ b;
}
public String getRGB(){
return rgb;
}
//你可以添加这样的方法
public int getRed(){
return r;
}
public int getGreen(){
return g;
}
public int getBlue(){
return r;
}
//甚至这些
public Color getColor(){
return new Color(r,g,b);
}
public int getARGB(){
return 0xFF000000 | ((r <16)& 0x00FF0000)| ((g <8)& 0x0000FF00)| b;
}
}
通过单独存储三个组件(和整数)您可以对他们进行很多有用的操作。
请注意,可以轻松地和其他方法(如将其作为单个ARGB整数进行检索),可以单独提取三个组件更容易实现)。
I've never used enums before so I'm finding them very confusing! I'd like to store a lot of RGB values (as strings) and I assume an enum is the best option rather than a class listing a load of static final strings? I'm experimenting with the code and here's what I've got so far, is this correct? (Seems to work OK)
public enum Colors {
GREY("142, 142, 147"),
RED("255, 59, 48"),
GREEN("76, 217, 100"),
PURPLE("88, 86, 214"),
LIGHTBLUE ("52, 170, 220"); //... etc, this is a shorted list
private Colors(final String string) {
this.string = string;
}
private final String string;
public String getRGB() {
return string;
}
}
public class HelloWorld{
public static void main(String[] args) {
String test = Colors.LIGHTBLUE.getRGB();
System.out.println(test);
}
}
Perhaps change it to the following:
public enum Colors {
GREY(142, 142, 147),
RED(255, 59, 48),
GREEN(76, 217, 100),
PURPLE(88, 86, 214),
LIGHTBLUE (52, 170, 220); //... etc, this is a shorted list
private final int r;
private final int g;
private final int b;
private final String rgb;
private Colors(final int r,final int g,final int b) {
this.r = r;
this.g = g;
this.b = b;
this.rgb = r + ", " + g + ", " + b;
}
public String getRGB() {
return rgb;
}
//You can add methods like this too
public int getRed(){
return r;
}
public int getGreen(){
return g;
}
public int getBlue(){
return r;
}
//Or even these
public Color getColor(){
return new Color(r,g,b);
}
public int getARGB(){
return 0xFF000000 | ((r << 16) & 0x00FF0000) | ((g << 8) & 0x0000FF00) | b;
}
}
By storing the three components separately (and as integers) you can do a lot of useful manipulations with them.
Note how the three components can be extracted separately with ease and additional methods (such as retrieving them as a single ARGB integer is much easier to implement).
这篇关于枚举存储RGB字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!