vaab / colour

Python color representations manipulation library (RGB, HSL, web, ...)
BSD 2-Clause "Simplified" License
319 stars 41 forks source link

Accept and return integer RGB values? #36

Open tony opened 7 years ago

tony commented 7 years ago

Is there a chance integer RGB values can be accepted? It makes compatibility with PIL's color signatures more streamlined.

giswqs commented 4 years ago

I second this. It would be nice to support integer RGB values from 0 to 255.

giswqs commented 4 years ago

I wrote a function for accepting integer RGB values. It is not perfect, but it gets the job done for me.

from colour import Color

def check_color(in_color):

    out_color = ''
    if isinstance(in_color, tuple) and len(in_color) == 3:
        if all(isinstance(item, int) for item in in_color):
            rescaled_color = [x / 255.0 for x in in_color]
            out_color = Color(rgb=tuple(rescaled_color))
            return out_color
        else: 
            print('RGB color must be a tuple with three integer values ranging from 0 to 255.')
            return
    else:
        try:
            out_color = Color(in_color)
            return out_color
        except Exception as e:
            print('The provided color is invalid.')
            print(e)
            return