Skip to content

Instantly share code, notes, and snippets.

@defims
Forked from xpansive/LICENSE.txt
Created June 14, 2016 02:36
Show Gist options
  • Save defims/7a536e6af32d01b0d6aa0660bd5c8f37 to your computer and use it in GitHub Desktop.
Save defims/7a536e6af32d01b0d6aa0660bd5c8f37 to your computer and use it in GitHub Desktop.
HSV to RGB in 107 bytes

HSV to RGB in 107 bytes

I'm kinda new to this and I'm sure some of you will find a stranger/smaller solution... Golf away!

function(
//parameters range from 0 - 1
a, // hue 0.0 - 1.0
b, // saturation 0.0 - 1.0
c, // value 0.0 - 1.0
d, // hue divider
e, // value splitter
f // array placeholder
){
d=a*6%1; // deviation of the current hue (red<->yellow<->green<->cyan<->blue<->violet),
// the higher the value, the more the color deviates to the next hue
// create an array of the color strength regardless of hue
f=[
c, // full value on this color
-c*d*b+c, // deviation part 1
e=-c*b+c, // deviation part 2
e, // median deviation
c*d*b+e, // deviation part 3
c // full value
];
// select the colors out of the array in regard of hue
return[ // [r,g,b] - ranges from 0 to 1
f[d=a*6|0], //red
f[(4+d)%6], //green
f[(2+d)%6] //blue
]
}
export function(a,b,c,d,e,f){d=a*6%1;f=[c,-c*d*b+c,e=-c*b+c,e,c*d*b+e,c];return[f[d=a*6|0],f[(4+d)%6],f[(2+d)%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": "hsvToRgb",
"description": "Converts colors from HSV to RGB.",
"keywords": [ "color", "rgb", "hsv" ],
"version": "0.1.0"
}
<!DOCTYPE html>
<title>hsvToRgb Test</title>
<canvas id="canvas"></canvas>
<script>
var hsvToRgb = function(a,b,c,d,e,f){d=a*6%1;f=[c,-c*d*b+c,e=-c*b+c,e,c*d*b+e,c];return[f[d=a*6|0],f[(4+d)%6],f[(2+d)%6]]}
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var size = canvas.width = canvas.height = 150;
var imageData = ctx.createImageData(size, size);
var pixels = imageData.data;
var hue = 0;
setInterval(function() {
var hue = new Date/4000;
for (var i = size * size * 4; i -= 4;) {
var c = hsvToRgb(hue % 1, i / 4 % size / size, i / 4 / size / size);
pixels[i] = c[0] * 255;
pixels[i + 1] = c[1] * 255;
pixels[i + 2] = c[2] * 255;
pixels[i + 3] = 255;
}
ctx.putImageData(imageData, 0, 0);
}, 50)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment