1

If you had a form where you in put hex colour codes

FFFFFF

how would you determine their hue? Like Cyan, Orange, etc...?

So if someone typed in

#FF8000

The hue would be printed on the page saying Orange.

If possible, could you also explain how this works? Or link me to something that explains it?

Would it be plausible to enter this code inside of my main python script to be able to convert to HSV?

        #RGB to HSV start
    ONE_THIRD = 1.0/3.0
    ONE_SIXTH = 1.0/6.0
    TWO_THIRD = 2.0/3.0
    def rgb_to_hsv(r, g, b):
        maxc = max(r, g, b)
        minc = min(r, g, b)
        v = maxc
        if minc == maxc:
            return 0.0, 0.0, v
            s = (maxc-minc) / maxc
            rc = (maxc-r) / (maxc-minc)
            gc = (maxc-g) / (maxc-minc)
            bc = (maxc-b) / (maxc-minc)
        if r == maxc:
            h = bc-gc
        elif g == maxc:
            h = 2.0+rc-bc
        else:
            h = 4.0+gc-rc
            h = (h/6.0) % 1.0
            return h, s, v

    #RGB to HSV end
Aj Entity
  • 4,741
  • 4
  • 17
  • 13
  • 3
    Check this out: http://stackoverflow.com/questions/2453344/find-the-colour-name-from-a-hexadecimal-colour-code – wquist Jun 16 '12 at 02:14
  • My understanding is that you want a mapping from hex codes to descriptive color terms, is that correct, or am I misunderstanding what you are trying to do? – Levon Jun 16 '12 at 02:40
  • Please see my comment below. There's no reason you couldn't put a function from a library directly in your code, but why would you when you can just import the library and call it as I've shown you below? Much cleaner. – Dan Jun 16 '12 at 02:42
  • @user37078 Just so I can pinpoint and find out what each thing does. If I imported a file, I would have to read the two files side by side. And I would not be converting into any other value listed in there. Just making it easy on myself I guess. – Aj Entity Jun 16 '12 at 02:45
  • @Levon Yes, pretty much. Only thing is, I just want the basic ones. I already have Red, Green, and Blue understood. I am just trying to pinpoint when a red is not a red anymore if you add green, and that it becomes an "orange-ish" colour. Same with blue. If you add enough red, but not too much, it will become a magenta. Or if you get a good balance, it will be purple. I just want to point those things out. The basic colours such as Yellow, Orange, Magenta, Purple, Cyan, etc... – Aj Entity Jun 16 '12 at 02:48
  • My answer [here](http://stackoverflow.com/questions/9694165/convert-rgb-color-to-english-color-name-like-green/9694246#9694246) might help a little. – fraxel Jun 16 '12 at 08:32

3 Answers3

3

Please see colorsys.

Basically, it works like this:

>>> import colorsys
>>> colorsys.rgb_to_hsv(.3, .4, .2)
(0.25, 0.5, 0.4)
Dan
  • 3,665
  • 1
  • 31
  • 39
  • Would it be plausible to just put this code inside the individual python script instead? – Aj Entity Jun 16 '12 at 02:31
  • That looks messy, I will edit the code inside my original question. – Aj Entity Jun 16 '12 at 02:33
  • It's hard to read a big code dump in a comment, but it looks like you're trying to make a function that pretty much does the same thing as an included python library. Why would you bother? – Dan Jun 16 '12 at 02:36
  • Just so I can pinpoint and find out what each thing does. If I imported a file, I would have to read the two files side by side. And I would not be converting into any other value listed in there. Just making it easy on myself I guess. – Aj Entity Jun 16 '12 at 02:40
  • What are you trying to do exactly? Are you new to python and programming in general? My advice would be to get accustomed to having code in different files, it's a practical necessity in the life of a programmer. – Dan Jun 16 '12 at 02:46
  • I just started Python under 1 week ago. Never programmed anything in my life before that. – Aj Entity Jun 16 '12 at 02:49
  • Cool. Yeah I would just say unless you're doing it for educational purposes, you want to get used to the pattern of importing code and calling it. Good luck. – Dan Jun 16 '12 at 02:54
  • Thank you. :D And yea. Pretty much the same concept as using stylesheets I guess. But right now I am trying to find out "how" the method words instead of just "making it work." Normally, I would use the traditional reading of every single tutorial, but as I have Criminology midterms coming up, I have enough reading to do as it is. :P – Aj Entity Jun 16 '12 at 02:56
  • I will mark this as my answer, since this pretty much did what I wanted it to do. – Aj Entity Jun 16 '12 at 03:02
1

I'm not sure there's an easy algorithmic way to solve this to come up with accurate and descriptive color designations. I think some sort of look-up would be required.

Unless you have access to a database of hex codes/descriptions already, one approach would be using a dictionary where your keys would be your hex codes, and the values would be the corresponding descriptions is one way to go. You could look up the hex code and display the corresponding color description.

You could find the values for your dictionary from any number of web pages that specialize on that sort type of color information such as Hexadecimal Color Code Chips Table (Hue at 10° of separation) or this Hexadecimal Color Codes

Though given the large number of hex codes, I'm not sure how feasible it would be for you to build this yourself from scratch.

Levon
  • 138,105
  • 33
  • 200
  • 191
  • Well, if you are asking am I already able to describe FF0000 as red, or FFFF00 as yellow, then yes. I do have that. – Aj Entity Jun 16 '12 at 02:33
  • @AjEntity Yes, the basic colors will be easy, I think the tricky part would be the various grades/hues of colors, no? – Levon Jun 16 '12 at 02:36
  • Yes. The grades/hues is what stumped me for the last 3 days. But I was just asking to make sure what the question was. – Aj Entity Jun 16 '12 at 02:44
1

using the file webcolors.txt in Tools\pynche you can create your own function which will return the color name of passed hex value:

with open ('Tools\pynche\webcolors.txt') as f:
      color_dict={x.split()[1].strip():x.split()[0].strip() for x in f.readlines()[1:]}

color_dict['fetch']=lambda x:color_dict.get(x.lower(),'color not found')


print( color_dict['fetch']('#ffFF00') )
print( color_dict['fetch']('#AAdd00') )
print(color_dict['fetch']('#FFFFFF'))

output:

Yellow
color not found
White
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504