枚举存储RGB字符串 [英] enum to store RGB strings

查看:188
本文介绍了枚举存储RGB字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用枚举,所以我发现他们很混乱!我想存储很多RGB值(作为字符串),我假设枚举是最好的选项,而不是列出静态最终字符串的负载的类?我在试验代码,这里是我到目前为止,这是正确的吗? (似乎可以正常工作)

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆