Search code examples
javapythonenumsclass-constructors

Enumerations With Functions and Constructors in Python


I am learning Python and am making a boundary for the PyGame library. There is nothing wrong with it, I just want to make methods and classes more suited to my specific project. In short, I want to make a color enumeration, but each value in the enumeration has an RGB and Hex version. Since I've done this in Java before, I'll show roughly what I mean in a Java enum.

public enum Color {

    RED(new double[]{255, 0, 0}, "#ff0000"),
    CYAN(new double[]{0, 255, 255}, "#00e1ff");
    // Other colors...

    double[] rgb;
    String hex;

    Color(double[] rgb, String hex) {
        this.rgb = rgb;
        this.hex = hex;
    }

    public double[] getRGB() {
        return rgb;
    }

    public String getHex() {
        return hex;
    }
}

All I'm doing here is making a constructor (private by default in Java) for the enumeration and making each item in the enumeration specify the parameters in that constructor. RED, for example, specifies that the rgb double array is equal to {255, 0, 0} through the constructor and the hex string is "ff0000". With the methods at the bottom, you can retrieve these values anywhere in the code.

In the end, if I import the enumeration into a code, and then use the RED item, I can say RED.getRGB() and get a double array. I want to add a bunch of colors in this enumeration but I also want to know the syntax for this in general because it applies to other parts of my program.

I want to do this in Python. I barely understand making regular enums in Python and don't understand the whole "class Clazz(Enum):" thing, but I certainly don't know how to do the code above in Python. I just don't know the syntax.


Solution

  • dont overthink it

    class Color:
         def __init__(self,r,g,b):
            self.r = r
            self.g = g
            self.b = b
    
         def __str__(self):
             # return the string repr
             return f"#{self.r:02X}{self.g:02X}{self.b:02X}"
    
         def __getitem__(self,item):
             # allow `color[0],color[1],color[2]` access
             return [self.r,self.g,self.b][item]
    
         def __iter__(self):
             # cast to list
             return iter([self.r,self.g,self.b])
         
    class Colors:
      RED = Color(255,0,0)
      GREEN = Color(0,255,0)
      BLUE = Color(0,0,255)
      WHITE = Color(255,255,255)
      BLACK = Color(0,0,0)
      
    print(list(Colors.RED))
    print(Colors.GREEN[1])
    print("%s"%Colors.BLUE)
    

    If you really want to use that enum class you can as follows

    from enum import Enum
    
    class Color:
         def __init__(self,r,g,b):
            self.r = r
            self.g = g
            self.b = b
    
         def toHex(self):
             return f"#{self.r:02X}{self.g:02X}{self.b:02X}"
         
         def toRGB(self):
             return (self.r,self.g,self.b)
    
    class Colors(Enum):
      RED = Color(255,0,0)
      GREEN = Color(0,255,0)
      BLUE = Color(0,0,255)
      WHITE = Color(255,255,255)
      BLACK = Color(0,0,0)
      
    print(Colors.RED)
    print(Colors.GREEN.value.toRGB())
    print("%s"%Colors.BLUE.value.toHex())