0

I have a list of color names:

bunchofcolornames = [
            "aliceblue",
            "antiquewhite",
            "aqua",
            "cyan"] #this list is actually much longer...

For my matplotlib plots, I want to select those, which appear rather dark on a white background. For this I'd like to get the shade value in the HSV representation of that color name. How to do this best?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
asPlankBridge
  • 1,032
  • 1
  • 17
  • 30

2 Answers2

1

I don't know python, but I can answer the language-agnostic part of this:

The value part of RGB to HSV is the simplest part; it's the maximum of the R, G and B component divided by 255.0. (The HSL and HSI representations are slightly different with HSL averaging the largest and smallest of the three RGB components and HSI averaging all three).

For predicting contrast, you might be better off using luma, which for the sRGB colourspace is R ÷ 255.0 × 0.21 + G ÷ 255.0 × 0.72 + B ÷ 255.0 × 0.07. For other colourspaces it's slightly different (e.g with NTSC televisions it's R ÷ 255.0 × 0.3 + G ÷ 255.0 × 0.59 + B ÷ 255.0 × 0.11), but for guessing whether to pair with black or white, just sticking with sRGB should give reasonable results.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

Check out the color package which supports converting color names to HSL.

arocks
  • 2,862
  • 1
  • 12
  • 20
  • Although the `color` package is indeed exactly what the OP needs, you could have provided a short sample showing how that library provides exactly that translation. As it stands, your answer could have been a comment, as it contains nothing more than a link. – Martijn Pieters Jan 21 '14 at 15:26