Search code examples
pythonimage-processingcolorscolor-theorycolor-wheel

is there a coding way to check whether a color palette follows the color schemes of a color wheel like Analogous, monochromatic, triadic etc..?


I am eager to build a system which tells whether my color palette follows any color schemes from the color wheel, i.e., monochromatic, analogous, complementary, split complementary, triadic, square, and rectangle (or tetradic).

I have been thinking about this problem since last few days but couldn't come up with something.

I don't have that much clues about it as to where to start, please if I can get some initial ideas as to how to proceed using python.


Solution

  • I'm no colour-theorist and will happily remove my suggestion if someone with knowledge/experience contributes something more professional.

    I guess you want to convert to HSL/HSV colourspace, which you can do with ImageMagick or OpenCV very simply.

    • For monochromatic, you'd look at the Hue channel (angle) and see if all the Hues are within say 10-15 degrees of each other.

    • For complementary, you'd be looking for 2 groups of Hue angles around 180 degrees apart.

    • For triadic, 3 clusters of angles separated by around 120 degrees. And so on. I don't know the more exotic schemes.

    You can get HSV with OpenCV like this:

    import cv2
    
    # Open image as BGR
    im = cv2.imread(XXX)
    
    # Convert to HSV
    HSV = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    

    Bear in mind that OpenCV Hue angles are half the conventional values so that the 0..360 range fits inside an unsigned 8-bit integer as 0..180 if dealing with 8-bit images. Range is the conventional 0..360 when dealing with floats.


    Here is a little example with a colour wheel, where I split the image into Hue, Saturation and Value with ImageMagick and then lay out the channels beside each other with Hue on the left, Saturation in the centre and Value on the right:

    magick colorwheel.png -colorspace HSV -separate +append separated.png
    

    Input Image

    enter image description here

    Separated Image

    enter image description here

    Hopefully you can see that the Hue values go around the colour wheel in the left panel, that the Saturation decreases as you move away from the centre and that the Value increases as you move out radially.

    You can hover a colour picker over, say, the green tones and see that they all have similar Hue. So, if you were looking for complementary colours. hover over the two and see if they are 180 degrees of Hue apart, for example.