/** * @module color-space/hsv */ 'use strict' var rgb = require('./rgb'); var hsl = require('./hsl'); module.exports = { name: 'hsv', min: [0,0,0], max: [360,100,100], channel: ['hue', 'saturation', 'value'], alias: ['HSV', 'HSB'], rgb: function(hsv) { var h = hsv[0] / 60, s = hsv[1] / 100, v = hsv[2] / 100, hi = Math.floor(h) % 6; var f = h - Math.floor(h), p = 255 * v * (1 - s), q = 255 * v * (1 - (s * f)), t = 255 * v * (1 - (s * (1 - f))); v *= 255; switch(hi) { case 0: return [v, t, p]; case 1: return [q, v, p]; case 2: return [p, v, t]; case 3: return [p, q, v]; case 4: return [t, p, v]; case 5: return [v, p, q]; } }, hsl: function(hsv) { var h = hsv[0], s = hsv[1] / 100, v = hsv[2] / 100, sl, l; l = (2 - s) * v; sl = s * v; sl /= (l <= 1) ? l : 2 - l; sl = sl || 0; l /= 2; return [h, sl * 100, l * 100]; } }; //append rgb rgb.hsv = function(rgb) { var r = rgb[0], g = rgb[1], b = rgb[2], min = Math.min(r, g, b), max = Math.max(r, g, b), delta = max - min, h, s, v; if (max === 0) { s = 0; } else { s = (delta/max * 100); } if (max === min) { h = 0; } else if (r === max) { h = (g - b) / delta; } else if (g === max) { h = 2 + (b - r) / delta; } else if (b === max) { h = 4 + (r - g) / delta; } h = Math.min(h * 60, 360); if (h < 0) { h += 360; } v = ((max / 255) * 1000) / 10; return [h, s, v]; }; //extend hsl hsl.hsv = function(hsl) { var h = hsl[0], s = hsl[1] / 100, l = hsl[2] / 100, sv, v; l *= 2; s *= (l <= 1) ? l : 2 - l; v = (l + s) / 2; sv = (2 * s) / (l + s) || 0; return [h, sv * 100, v * 100]; };