Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum to store RGB strings [closed]

Tags:

java

enums

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);

    }
}
like image 875
Mateus Avatar asked Apr 10 '26 12:04

Mateus


2 Answers

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).

like image 134
initramfs Avatar answered Apr 13 '26 03:04

initramfs


Code looks good, but better would be to keep RGB values as number (because they are number indeed). You could re-write code like:

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 Integer red, final Integer green, final Integer blue) {
    this.red = red;
    this.green = green;
    this.blue = blue;
}

private final Integer red, green, blue;

public String getRGB() {
    return red + "," + green + "," + blue;
}
}

public class HelloWorld{
public static void main(String[] args) {

    String test = Colors.LIGHTBLUE.getRGB();
    System.out.println(test);

}
}
like image 37
Gyanendra Dwivedi Avatar answered Apr 13 '26 03:04

Gyanendra Dwivedi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!