Skip to content

Instantly share code, notes, and snippets.

@NV
Created August 13, 2010 11:40
Show Gist options
  • Select an option

  • Save NV/522734 to your computer and use it in GitHub Desktop.

Select an option

Save NV/522734 to your computer and use it in GitHub Desktop.
function hsb2hsl(h, s, b) {
var hsl = {h:h};
hsl.l = (2 - s) * b;
hsl.s = s * b;
if (hsl.l <= 1 && hsl.l > 0) {
hsl.s /= hsl.l;
} else {
hsl.s /= 2 - hsl.l;
}
hsl.l /= 2;
if (hsl.s > 1) {
hsl.s = 1;
}
return hsl;
}
function eql(a, b) {
if (a != b) {
console.error("Expected: " + b + ". Actual: "+ a)
}
}
eql( hsb2hsl(0, 0, 0).h, 0)
eql( hsb2hsl(0, 0, 0).s, 0)
eql( hsb2hsl(0, 0, 0).l, 0)
eql( hsb2hsl(0, 0, 1).h, 0)
eql( hsb2hsl(0, 0, 1).s, 0)
eql( hsb2hsl(0, 0, 1).l, 1)
eql( hsb2hsl(0, 0.5450980392156862, 1).s, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment