About Talks Articles

Converting between HSL and HSV

1 min read

Amazingly, tons of code fragments on how to convert between RGB and HSV as well as between RGB and HSL do exist (in any imagineable programming languages). However, googling for HSL to HSV conversion did not reveal anything useful. Maybe I was blind, but there is really no point wasting minutes for something that (should be trivial). A quick glance at the wikipedia article on HSL and HSV gave me the following code:

void hsv_to_hsl(double h, double s, double v,
double* hh, double* ss, double *ll)
    *hh = h;
    *ll = (2 - s) * v;
    *ss = s * v;
    *ss /= (*ll < = 1) ? (*ll) : 2 - (*ll);
    *ll /= 2;
void hsl_to_hsv(double hh, double ss, double ll,
double* h, double* s, double *v)
    *h = hh;
    ll *= 2;
    ss *= (ll <= 1) ? ll : 2 - ll;
    *v = (ll + ss) / 2;
    *s = (2 * ss) / (ll + ss);

Error checking is left as an exercise to the reader, corrections are welcomed. If the code seems to be cryptic, then there is a reason to take a napkin and jot some stuff there…

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook Google+

comments powered by Disqus