-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix colormin function and add colormin tests
- Loading branch information
Showing
9 changed files
with
197 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
npm-debug.log | ||
dist |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
import test from 'tape'; | ||
import min from '../lib/colormin'; | ||
|
||
test('should return the smallest colour', t => { | ||
t.plan(20); | ||
t.equal(min('RED'), 'red', 'should lowercase keywords'); | ||
t.equal(min('#f00'), 'red', 'should convert shorthand hex to keyword'); | ||
t.equal(min('#ff0000'), 'red', 'should convert longhand hex to keyword'); | ||
t.equal(min('rgb', [255, 0, 0]), 'red', 'should convert rgb to keyword'); | ||
t.equal(min('rgba', [255, 0, 0, 1]), 'red', 'should convert fully opaque rgb to keyword'); | ||
t.equal(min('hsl', [0, 100, 50]), 'red', 'should convert hsl to keyword'); | ||
t.equal(min('hsla', [0, 100, 50, 1]), 'red', 'should convert fully oqaque hsl to keyword'); | ||
t.equal(min('hsla', [0, 100, 50, 0.5]), 'rgba(255,0,0,.5)', 'should convert translucent hsla to rgba'); | ||
t.equal(min('#FFFFFF'), '#fff', 'should convert longhand hex to shorthand, case insensitive'); | ||
t.equal(min('WHiTE'), '#fff', 'should convert keyword to hex, case insensitive'); | ||
t.equal(min('yellow'), '#ff0', 'should convert keyword to hex'); | ||
t.equal(min('rgb', [12, 134, 29]), '#0c861d', 'should convert rgb to hex'); | ||
t.equal(min('hsl', [230, 50, 40]), '#349', 'should convert hsl to hex'); | ||
t.equal(min('#000080'), 'navy', 'should convert another longhand hex to keyword'); | ||
t.equal(min('rgba', [199, 190, 179, 0.8]), 'hsla(33,15%,74%,.8)', 'should convert rgba to hsla when shorter'); | ||
t.equal(min('rgba', [0, 0, 0, 0]), 'transparent', 'should convert this specific rgba value to "transparent"'); | ||
t.equal(min('hsla', [0, 0, 0, 0]), 'transparent', 'should convert this specific hsla value to "transparent"'); | ||
t.equal(min('hsla', [200, 0, 0, 0]), 'transparent', 'should convert hsla values with 0 saturation & 0 lightness to "transparent"'); | ||
t.equal(min('transparent'), 'transparent', 'should leave transparent as it is'); | ||
t.equal(min('#696969'), '#696969', 'should prefer to output hex rather than keywords when they are the same length'); | ||
}); | ||
|
||
test('should pass through if not recognised', t => { | ||
t.plan(2); | ||
t.equal(min('Unrecognised'), 'Unrecognised'); | ||
t.equal(min('inherit'), 'inherit'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
import convert from 'colr-convert'; | ||
import keywords from 'css-color-names'; | ||
import dropLeadingZero from './drop-leading-zero'; | ||
import {isHex, toShorthand, toLonghand} from './hex'; | ||
let hexes = {}; | ||
let round = Math.round; | ||
|
||
Object.keys(keywords).forEach(keyword => hexes[keywords[keyword]] = keyword); | ||
|
||
let shorter = (a, b) => (a && a.length < b.length ? a : b); | ||
|
||
export default function (name, args) { | ||
var word = name.toLowerCase(); | ||
if (word === 'rgb' || word === 'rgba' || word === 'hsl' || word === 'hsla') { | ||
if (!args) { | ||
return name; | ||
} | ||
if (word[3] === 'a') { | ||
if (args[1] === 0 && args[2] === 0 && args[3] === 0) { | ||
if (word === 'hsla' || args[0] === 0) { | ||
return 'transparent'; | ||
} | ||
} | ||
if (args[3] === 1) { | ||
word = word.slice(0, 3); | ||
} else { | ||
args[3] = dropLeadingZero(args[3]); | ||
} | ||
} | ||
if (word === 'hsl') { | ||
word = 'rgb'; | ||
args = convert.hsl.rgb(args); | ||
args[0] = round(args[0]); | ||
args[1] = round(args[1]); | ||
args[2] = round(args[2]); | ||
} | ||
if (word === 'rgb') { | ||
word = convert.rgb.hex(args); | ||
} else { | ||
let rgba, hsla; | ||
// alpha conversion | ||
if (word === 'rgba') { | ||
rgba = args; | ||
hsla = convert.rgb.hsl(args); | ||
hsla[0] = round(hsla[0]); | ||
hsla[1] = round(hsla[1]); | ||
hsla[2] = round(hsla[2]); | ||
hsla.push(args[3]); | ||
} else { | ||
hsla = args; | ||
rgba = convert.hsl.rgb(args); | ||
rgba[0] = round(rgba[0]); | ||
rgba[1] = round(rgba[1]); | ||
rgba[2] = round(rgba[2]); | ||
rgba.push(args[3]); | ||
} | ||
hsla[1] = hsla[1] + '%'; | ||
hsla[2] = hsla[2] + '%'; | ||
return shorter(`hsla(${hsla.join()})`, `rgba(${rgba.join()})`); | ||
} | ||
} | ||
|
||
if (isHex(word)) { | ||
word = toLonghand(word); | ||
if (word in hexes) { | ||
return shorter(hexes[word], toShorthand(word)); | ||
} | ||
return toShorthand(word); | ||
} | ||
|
||
if (word in keywords) { | ||
return shorter(toShorthand(keywords[word]), word); | ||
} | ||
|
||
return name; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
export default number => { | ||
let value = String(number); | ||
|
||
if (number % 1) { | ||
if (value[0] === '0') { | ||
return value.slice(1); | ||
} | ||
|
||
if (value[0] === '-' && value[1] === '0') { | ||
return '-' + value.slice(2); | ||
} | ||
} | ||
|
||
return value; | ||
} |
Oops, something went wrong.