Skip to content

Instantly share code, notes, and snippets.

@ciembor
Last active March 12, 2026 21:24
Show Gist options
  • Select an option

  • Save ciembor/1494530 to your computer and use it in GitHub Desktop.

Select an option

Save ciembor/1494530 to your computer and use it in GitHub Desktop.
RGB to HSL and HSL to RGB conversion functions
/*
* Copyright 2011 Maciej Ciemborowicz
* https://maciej-ciemborowicz.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef struct rgb {
float r, g, b;
} RGB;
typedef struct hsl {
float h, s, l;
} HSL;
/*
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns HSL in the set [0, 1].
*/
HSL rgb2hsl(float r, float g, float b) {
HSL result;
r /= 255;
g /= 255;
b /= 255;
float max = MAX(MAX(r,g),b);
float min = MIN(MIN(r,g),b);
result.h = result.s = result.l = (max + min) / 2;
if (max == min) {
result.h = result.s = 0; // achromatic
}
else {
float d = max - min;
result.s = (result.l > 0.5) ? d / (2 - max - min) : d / (max + min);
if (max == r) {
result.h = (g - b) / d + (g < b ? 6 : 0);
}
else if (max == g) {
result.h = (b - r) / d + 2;
}
else if (max == b) {
result.h = (r - g) / d + 4;
}
result.h /= 6;
}
return result;
}
////////////////////////////////////////////////////////////////////////
/*
* Converts an HUE to r, g or b.
* returns float in the set [0, 1].
*/
float hue2rgb(float p, float q, float t) {
if (t < 0)
t += 1;
if (t > 1)
t -= 1;
if (t < 1./6)
return p + (q - p) * 6 * t;
if (t < 1./2)
return q;
if (t < 2./3)
return p + (q - p) * (2./3 - t) * 6;
return p;
}
////////////////////////////////////////////////////////////////////////
/*
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns RGB in the set [0, 255].
*/
RGB hsl2rgb(float h, float s, float l) {
RGB result;
if(0 == s) {
result.r = result.g = result.b = l * 255; // achromatic
}
else {
float q = l < 0.5 ? l * (1 + s) : l + s - l * s;
float p = 2 * l - q;
result.r = hue2rgb(p, q, h + 1./3) * 255;
result.g = hue2rgb(p, q, h) * 255;
result.b = hue2rgb(p, q, h - 1./3) * 255;
}
return result;
}
@ciembor
Copy link
Author

ciembor commented Mar 7, 2023

It's the do what you want license :). I don't even know when I wrote this, it had to be at university. By the way, this is how I feel when I read your comment after 12 years:

crying

@alpheratz0
Copy link

Thanks for the fast reply!!!

It's the do what you want license :). I don't even know when I wrote this, it had to be at university.

Great license choise!!! =)

By the way, this is how I feel when I read your comment after 12 years:

crying

I hope the cat is crying for a good reason =(

P.S.: I think I spotted an error; shouldn't line 91

 if(0 == s) {
    result.r = result.g = result.b = l; // achromatic
  }

be:

 if(0 == s) {
    result.r = result.g = result.b = l * 255; // achromatic
  }

?

@Wdestroier
Copy link

Ten years ago, a StackOverflow response came to the aid of a legacy system maintainer. It's unclear how many individuals benefitted from this code snippet. Should @alpheratz0 and others engage in analytics? Time is of the essence; we must act quickly. Our efforts will ultimately be forgotten, our loved ones will pass away, and our identity will fade into obscurity. However, we can make a difference by helping someone today. Thank you for not being indifferent; your actions can have an impact.

@ciembor
Copy link
Author

ciembor commented Apr 12, 2023

@Wdestroier what are you talking about?

@ciembor
Copy link
Author

ciembor commented Apr 12, 2023

@alpheratz0 It's possible there is a mistake, does you code works for you? I can correct it.

@alpheratz0
Copy link

@ciembor it works if I change that line, the comment says...

 * Assumes h, s, and l are contained in the set [0, 1] and
 * returns RGB in the set [0, 255].

but when the saturation is 0, the function returns an RGB color in the range [0, 1]

@qShuan
Copy link

qShuan commented Sep 11, 2023

This is what I've been searching for, I can't thank you enough! ;))

@Kimiberry64
Copy link

looks kinda messy ngl

@ciembor
Copy link
Author

ciembor commented Sep 17, 2025

@sixtyfourLEC Another one πŸ˜‚. How did you even find it? That was a solution to an exercise from university or something I wrote during converting color schemes to the Geany editor. 14 years ago πŸ˜‚.

@ciembor
Copy link
Author

ciembor commented Sep 17, 2025

@alpheratz0 fixed πŸ˜‚. Since this code has become so popular, I see no other choice but to maintain it πŸ˜‚. I also added the Apache license comment at the beginning, that says what I said before.

@Kimiberry64
Copy link

@sixtyfourLEC Another one πŸ˜‚. How did you even find it? That was a solution to an exercise from university. 14 years ago πŸ˜‚.

i found it randomly lmfao

@ciembor
Copy link
Author

ciembor commented Sep 17, 2025

@sixtyfourLEC but on Google or how? And what is messy? I don't code in C for a long time, I know there were some changes since C99 :).

@Kimiberry64
Copy link

@sixtyfourLEC but on Google or how? And what is messy? I don't code in C for a long time, I know there were some changes since C99 :).

google yes, the code looks very messy

@ciembor
Copy link
Author

ciembor commented Sep 17, 2025

@sixtyfourLEC I'm glad you still use search engines instead of LLM, because I can learn something. But you must tell me what is messy πŸ˜‚.

very-strong-code

@alpheratz0
Copy link

@alpheratz0 fixed πŸ˜‚. Since this code has become so popular, I see no other choice but to maintain it πŸ˜‚.

lets go!

I also added the Apache license comment at the beginning, that says what I said before.

noooo RIP "do what you want" license

@plettj
Copy link

plettj commented Mar 11, 2026

I searched rgb to hsl cpp in google, and this gist was the first result. The search engines have seen you, @ciembor, and they bestow their blessings upon you.

@Maxi2022gt
Copy link

I searched rgb to hsl cpp in google, and this gist was the first result. The search engines have seen you, @ciembor, and they bestow their blessings upon you.

i remember searching it up too!

looks kinda messy ngl

if it ain't broke, don't fix it I'M SORRY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment