color-space
Advanced tools
Comparing version 1.14.2 to 1.14.3
132
hcg.js
/** | ||
* @module color-space/hcg | ||
*/ | ||
var rgb = require('./rgb'); | ||
var hsl = require('./hsl'); | ||
var hsv = require('./hsv'); | ||
var loop = require('mumath/mod'); | ||
var hwb = require('./hwb'); | ||
var mod = require('mumath/mod'); | ||
module.exports = { | ||
@@ -21,21 +23,28 @@ name: 'hcg', | ||
var g = hcg[2] / 100; | ||
if(c == 0.0) return [g * 255, g * 255, g * 255]; | ||
var hi = loop(h, 1) * 6; | ||
var v = loop(hi, 1); | ||
if (c === 0.0) { | ||
return [g * 255, g * 255, g * 255]; | ||
} | ||
var hi = mod(h, 1) * 6; | ||
var v = mod(hi, 1); | ||
var pure = [0, 0, 0]; | ||
var w = 1 - v; | ||
switch(Math.floor(hi)) { | ||
case 0 : pure[0] = 1; pure[1] = v; pure[2] = 0; break; | ||
case 1 : pure[0] = w; pure[1] = 1; pure[2] = 0; break; | ||
case 2 : pure[0] = 0; pure[1] = 1; pure[2] = v; break; | ||
case 3 : pure[0] = 0; pure[1] = w; pure[2] = 1; break; | ||
case 4 : pure[0] = v; pure[1] = 0; pure[2] = 1; break; | ||
default: pure[0] = 1; pure[1] = 0; pure[2] = w; | ||
switch (Math.floor(hi)) { | ||
case 0: | ||
pure[0] = 1; pure[1] = v; pure[2] = 0; break; | ||
case 1: | ||
pure[0] = w; pure[1] = 1; pure[2] = 0; break; | ||
case 2: | ||
pure[0] = 0; pure[1] = 1; pure[2] = v; break; | ||
case 3: | ||
pure[0] = 0; pure[1] = w; pure[2] = 1; break; | ||
case 4: | ||
pure[0] = v; pure[1] = 0; pure[2] = 1; break; | ||
default: | ||
pure[0] = 1; pure[1] = 0; pure[2] = w; | ||
} | ||
var mg = (1.0 - c) * g; | ||
var rgb = [ | ||
(c * pure[0] + mg) * 255, | ||
(c * pure[1] + mg) * 255, | ||
(c * pure[2] + mg) * 255 | ||
(c * pure[0] + mg) * 255, | ||
(c * pure[1] + mg) * 255, | ||
(c * pure[2] + mg) * 255 | ||
]; | ||
@@ -50,4 +59,4 @@ return rgb; | ||
var s = 0; | ||
if(l < 1.0 && l > 0.0){ | ||
if(l < 0.5){ | ||
if (l < 1.0 && l > 0.0) { | ||
if (l < 0.5) { | ||
s = c / (2 * l); | ||
@@ -59,4 +68,4 @@ } else { | ||
return [hcg[0], s * 100, l * 100]; | ||
}, | ||
}, | ||
hsv: function(hcg){ | ||
@@ -66,8 +75,17 @@ var c = hcg[1] / 100; | ||
var v = c + g * (1.0 - c); | ||
if(v > 0.0){ | ||
var res; | ||
if (v > 0.0) { | ||
var f = c / v; | ||
return [hcg[0], f * 100, v * 100]; | ||
res = [hcg[0], f * 100, v * 100]; | ||
} else { | ||
return [hcg[0], 0, v * 100]; | ||
res = [hcg[0], 0, v * 100]; | ||
} | ||
return res; | ||
}, | ||
hwb: function(hcg){ | ||
var c = hcg[1] / 100; | ||
var g = hcg[2] / 100; | ||
var v = c + g * (1.0 - c); | ||
return [hcg[0], (v - c) * 100, (1 - v) * 100]; | ||
} | ||
@@ -82,18 +100,26 @@ }; | ||
var b = rgb[2] / 255; | ||
var max = Math.max(r, g, b); | ||
var min = Math.min(r, g, b); | ||
var max = Math.max(Math.max(r, g), b); | ||
var min = Math.min(Math.min(r, g), b); | ||
var chroma = (max - min); | ||
var grayscale, hue; | ||
if(chroma < 1){ | ||
var grayscale; | ||
var hue; | ||
if (chroma < 1) { | ||
grayscale = min / (1 - chroma); | ||
} else grayscale = 0; | ||
if(chroma > 0){ | ||
hue = ( | ||
loop( | ||
(max == r ? loop((g - b) / chroma, 6) : | ||
(max == g ? ((b - r) / chroma) + 2 : | ||
((r - g) / chroma) + 4) | ||
) / 6), 1); | ||
} else hue = 0; | ||
} else { | ||
grayscale = 0; | ||
} | ||
if (chroma > 0) { | ||
if (max === r) { | ||
hue = mod((g - b) / chroma, 6); | ||
} else | ||
if (max === g) { | ||
hue = 2 + (b - r) / chroma; | ||
} else { | ||
hue = 4 + (r - g) / chroma; | ||
} | ||
hue /= 6; | ||
hue = mod(hue, 1); | ||
} else { | ||
hue = 0; | ||
} | ||
return [hue * 360, chroma * 100, grayscale * 100]; | ||
@@ -107,3 +133,3 @@ }; | ||
var c = 0; | ||
if(l < 0.5) { | ||
if (l < 0.5) { | ||
c = 2.0 * s * l; | ||
@@ -113,8 +139,10 @@ } else { | ||
} | ||
if(c < 1.0){ | ||
var res; | ||
if (c < 1.0) { | ||
var f = (l - 0.5 * c) / (1.0 - c); | ||
return [hsl[0], c * 100, f * 100]; | ||
res = [hsl[0], c * 100, f * 100]; | ||
} else { | ||
return [hsl[0], c * 100, 0]; | ||
res = [hsl[0], c * 100, 0]; | ||
} | ||
return res; | ||
}; | ||
@@ -127,8 +155,24 @@ | ||
var c = s * v; | ||
if(c < 1.0){ | ||
var res; | ||
if (c < 1.0) { | ||
var f = (v - c) / (1 - c); | ||
return [hsv[0], c * 100, f * 100]; | ||
res = [hsv[0], c * 100, f * 100]; | ||
} else { | ||
return [hsv[0], c * 100, 0]; | ||
res = [hsv[0], c * 100, 0]; | ||
} | ||
return res; | ||
} | ||
//extend hwb | ||
hwb.hcg = function(hwb){ | ||
var w = hwb[1] / 100; | ||
var b = hwb[2] / 100; | ||
var v = 1 - b; | ||
var c = v - w; | ||
var g = 0; | ||
if (c < 1) { | ||
g = (v - c) / (1 - c); | ||
} | ||
return [hwb[0], c * 100, g * 100]; | ||
} |
{ | ||
"name": "color-space", | ||
"description": "Color space conversions and data", | ||
"version": "1.14.2", | ||
"version": "1.14.3", | ||
"author": "Deema Yvanow <dfcreative@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -86,6 +86,8 @@ # Color-space [![Build Status](https://travis-ci.org/scijs/color-space.svg?branch=master)](https://travis-ci.org/scijs/color-space) [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) | ||
* [x] [HCG](https://github.com/acterhd/hcg-color) | ||
* [ ] [HSP](http://alienryderflex.com/hsp.html) | ||
* [ ] [HCY](http://chilliant.blogspot.ca/2012/08/rgbhcy-in-hlsl.html) | ||
## Contribute | ||
Please fork, add color space with basic conversions to/from XYZ or RGB and tests. | ||
Please fork, add color space with basic _conversions_ to/from XYZ or RGB and _tests_. | ||
The goal of project is to provide the most complete set of color spaces with maximally minimal uniform API. | ||
@@ -92,0 +94,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
111
0
61922
38
2114