Skip to content

Commit

Permalink
Update deps, make back-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Jan 25, 2022
1 parent 8816971 commit a4766f5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 39 deletions.
33 changes: 26 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
'use strict'

var parse = require('color-parse')
var rgb = require('color-space/rgb')
var hsl = require('color-space/hsl')

module.exports = function rgba (color) {
// template literals
Expand All @@ -17,19 +15,40 @@ module.exports = function rgba (color) {

if (!parsed.space) return []

var min = parsed.space[0] === 'h' ? hsl.min : rgb.min
var max = parsed.space[0] === 'h' ? hsl.max : rgb.max
var min = [0,0,0], max = parsed.space[0] === 'h' ? [360,100,100] : [255,255,255]

values = Array(3)
values[0] = Math.min(Math.max(parsed.values[0], min[0]), max[0])
values[1] = Math.min(Math.max(parsed.values[1], min[1]), max[1])
values[2] = Math.min(Math.max(parsed.values[2], min[2]), max[2])

if (parsed.space[0] === 'h') {
values = hsl.rgb(values)
}
if (parsed.space[0] === 'h') values = hsl2rgb(values)

values.push(Math.min(Math.max(parsed.alpha, 0), 1))

return values
}


// excerpt from color-space/hsl
function hsl2rgb(hsl) {
var h = hsl[0]/360, s = hsl[1]/100, l = hsl[2]/100, t1, t2, t3, rgb, val, i=0;

if (s === 0) return val = l * 255, [val, val, val];

t2 = l < 0.5 ? l * (1 + s) : l + s - l * s;
t1 = 2 * l - t2;

rgb = [0, 0, 0];
for (;i<3;) {
t3 = h + 1 / 3 * - (i - 1);
t3 < 0 ? t3++ : t3 > 1 && t3--;
val = 6 * t3 < 1 ? t1 + (t2 - t1) * 6 * t3 :
2 * t3 < 1 ? t2 :
3 * t3 < 2 ? t1 + (t2 - t1) * (2 / 3 - t3) * 6 :
t1;
rgb[i++] = val * 255;
}

return rgb;
}
65 changes: 36 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"browser": "index.js",
"module": "./index.mjs",
"scripts": {
"test": "node test.mjs"
"test": "node test.mjs && node test.cjs"
},
"exports": {
"import": "./index.mjs",
Expand All @@ -31,7 +31,7 @@
},
"homepage": "https://github.com/colorjs/color-rgba#readme",
"dependencies": {
"color-parse": "^1.4.1",
"color-space": "^1.14.6"
"color-parse": "^1.4.2",
"color-space": "^2.0.0"
}
}
25 changes: 25 additions & 0 deletions test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require( 'assert' )
var rgba = require( './index.js' )

assert.deepEqual(rgba('rgba(1,2,3,.5)'), [1,2,3,.5])
assert.deepEqual(rgba('rgba(0,0,0,0)'), [0,0,0,0])
assert.deepEqual(rgba('hsla(0,0,0,1)'), [0,0,0,1])
assert.deepEqual(rgba('rgba(-300,-300,-300,-1)'), [0,0,0,0])

assert.deepEqual(rgba('red'), [255, 0, 0, 1])
assert.deepEqual(rgba('rgb(80, 120, 160)'), [80, 120, 160, 1])
assert.deepEqual(rgba('rgba(80, 120, 160, .5)'), [80, 120, 160, .5])
assert.deepEqual(rgba('rgba(80 120 160 / .5)'), [80, 120, 160, .5])
assert.deepEqual(rgba('hsl(291 80% 50%)'), [198.89999999999995, 25.499999999999993, 229.5, 1])
assert.deepEqual(rgba('hsla(109, 50%, 50%, .75)'), [87.125, 191.25, 63.75, 0.75])
assert.deepEqual(rgba('#f00'), [255, 0, 0, 1])
assert.deepEqual(rgba`#f00`, [255, 0, 0, 1])

assert.deepEqual(rgba('xyz'), [])
// console.log(rgba('hsla(170, 50%, 45%, 1)'))

assert.deepEqual(rgba(0x00ff00), [0, 255, 0, 1])
assert.deepEqual(rgba(new Number(0x00ff00)), [0, 255, 0, 1])

assert.deepEqual(rgba([1,1,1,1]), [1,1,1,1])
assert.deepEqual(rgba(new Uint8Array([255, 255, 255, 255])), [255,255,255,1])

0 comments on commit a4766f5

Please sign in to comment.