Skip to content

Instantly share code, notes, and snippets.

@tylr
Created August 21, 2011 22:23
Show Gist options
  • Save tylr/1161267 to your computer and use it in GitHub Desktop.
Save tylr/1161267 to your computer and use it in GitHub Desktop.
DV.HSL = function(opts) {
var _defaults = {
hue: 0.0,
sat: 0.0,
lum: 0.0
};
this.config = $.extend(this._defaults, opts);
this.hue_to_rgb = function(p, q, 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;
};
this.to_hsl = function() {
return this.config;
};
this.to_rgb = function() {
var r, g, b;
if (this.config.sat == 0){
r = g = b = this.config.lum; // achromatic
} else {
var q = this.config.lum < 0.5 ? this.config.lum * (1 + this.config.sat) : this.config.lum + this.config.sat - this.config.lum * this.config.sat;
var p = 2 * this.config.lum - q;
r = this.hue_to_rgb(p, q, this.config.lum + 1/3);
g = this.hue_to_rgb(p, q, this.config.lum);
b = this.hue_to_rgb(p, q, this.config.lum - 1/3);
}
return {
'r': Math.round(r * 255),
'g': Math.round(g * 255),
'b': Math.round(b * 255)
};
};
};
DV.HSL.Slider = function(opts) {
var _defaults = {
elmnt: null,
val: 0.0
};
this.button = function() {
this.config.elmnt.children();
};
this.config = $.extend(this._defaults, opts);
};
$(document).ready(function() {
var fuck = new DV.HSL({
hue: 0.75,
sat: 0.1,
lum: 0.34
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment