Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created October 30, 2011 14:15
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aemkei/1325937 to your computer and use it in GitHub Desktop.
Save aemkei/1325937 to your computer and use it in GitHub Desktop.
hsl2rgb - 140byt.es

hsl2rgb - 140byt.es

This method converts color values from hue-saturation-lightness (HSL) to it's red-green-blue representation.

Check out the demo!

Special thanks to tsaniel, Alex Kloss, subzey, Jed Schmidt, and maettig for there unbelievable magic and effort!

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
a, // hue
b, // saturation
c // lightness
){
a *= 6;
b = [
c += b *= c < .5 ?
c :
1 - c,
c - a % 1 * b * 2,
c -= b *= 2,
c,
c + a % 1 * b,
c + b
];
return[
b[ ~~a % 6 ], // red
b[ (a|16) % 6 ], // green
b[ (a|8) % 6 ] // blue
]
}
function(a,b,c){a*=6;b=[c+=b*=c<.5?c:1-c,c-a%1*b*2,c-=b*=2,c,c+a%1*b,c+b];return[b[~~a%6],b[(a|16)%6],b[(a|8)%6]]}
// With proper error checking for all positive values. Saturation and lightness is clipped at 1.
function(a,b,c){a*=6;b=b>1||b;c=c>1||c;b=[c+=b*=c<.5?c:1-c,c-a%1*b*2,c-=b*=2,c,c+a%1*b,c+b];return[b[~~a%6],b[(a|16)%6],b[(a|8)%6]]}
// This version (137 bytes) uses a range from 0..360 for hue and 0..100 for saturation and lightness.
function(a,b,c){a/=60;c/=100;b=[c+=b*=(c<.5?c:1-c)/100,c-a%1*b*2,c-=b*=2,c,c+a%1*b,c+b];return[b[~~a%6],b[(a|16)%6],b[(a|8)%6]]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "hsl2rgb",
"description": "Converts hue-saturation-lightness to red-green-blue color value.",
"contributors": [
{
"name" : "Martin Kleppe",
"url" : "https://github.com/aemkei"
},
{
"name" : "tsaniel",
"url" : "https://github.com/tsaniel"
},
{
"name" : "Alex Kloss",
"url" : "https://github.com/atk"
},
{
"name" : "subzey",
"url" : "https://github.com/subzey"
},
{
"name": "Jed Schmidt",
"url": "https://github.com/jed"
},
{
"name": "maettig",
"url": "https://github.com/maettig"
}
],
"keywords": [
"color",
"convert",
"hsl",
"rgb"
]
}
<!DOCTYPE html>
<title>hsl2rgb - 140byt.es</title>
<style type="text/css" media="screen">
span {
display: inline-block;
width: 8px;
height: 8px;
}
#output {
margin-bottom: 1em;
}
#output div {
height: 8px;
}
</style>
<div id="output"></div>
<a href="https://gist.github.com/1325937">Source Code</a>
<script>
var hsl=function(a,b,c){a*=6;b=[c+=b*=c<.5?c:1-c,c-a%1*b*2,c-=b*=2,c,c+a%1*b,c+b];return[b[~~a%6],b[(a|16)%6],b[(a|8)%6]]}
var html = "", h, s, l, rgb;
for (h = 0; h <= 1; h += 0.1){
html += "<div>";
for (s = 0; s <= 1; s += 0.3){
for (l = 0; l <= 1; l += 0.1){
rgb = hsl(h, s, l);
rgb = [
~~(rgb[0] * 255),
~~(rgb[1] * 255),
~~(rgb[2] * 255)
].join(",")
html += "<span style='background:rgb(" + rgb + ")'></span>"
}
}
html += "</div>";
}
document.getElementById("output").innerHTML = html;
</script>
@12Me21
Copy link

12Me21 commented Jul 29, 2016

(h,s,l)=>{h*=6;s*=l<.5?l:1-l;H=++h|0;r[2]=0;r[H/2%3|0]=s*2;r[2-H%3]=Math.abs(h%2-1);return r.map(x=>((x+=l-s)<1?x:1)*255|0)}
124 bytes with actual RGB output (0-255). input is 0-1

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