How do you extract the hue component of a color given as #rrggbb?
How do you extract the hue component of a color given as #rrggbb ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you search for how to convert RGB to HSL, you’ll find a number of algorithms, including in the Wikipedia article linked by Sergey.
First, extract the RGB components of the hex color notation.
That’ll get you the byte (0-255) representation of your color. In this case, 199, 217, 44.
You can then use the formulae from the Wikipedia article to calculate hue, or shamelessly steal someone else’s code:
(See the source page for documentation and a
hslToRgb()function.)We can now put those two snippets together and get the hue:
The
[0]is to grab the hue – the function returns an array ([h,s,l]). We multiply by 360 since hue is returned as a value between 0 and 1; we want to convert it to degrees.With the example color of
#c7d92c,huewill be ~66.24. Photoshop’s color picker says the hue of that color is 66° so it looks like we’re good!