Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Alias gray[0] as white and fix incorrect spelling for fuchsia #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.nyc_output
coverage
dist
node_modules
3 changes: 1 addition & 2 deletions src/hue-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const names = [
'blue', // 210
'indigo', // 240
'violet', // 270
'fuschia', // 300
'fuchsia', // 300
'pink', // 330
'red', // 360
]
Expand All @@ -22,4 +22,3 @@ const hueName = h => {
}

module.exports = hueName

51 changes: 22 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,17 @@
const chroma = require('chroma-js')
const hueName = require('./hue-name')

const lums = [
9,
8,
7,
6,
5,
4,
3,
2,
1,
0
]
.map(n => n + .5)
const lums = Array(10).fill()
.map((x, i) => i)
.reverse()
.map(n => n + 0.5)
.map(n => n / 10)

const createArray = length => {
const arr = []
for (let i = 0; i < length; i++) {
arr.push(i)
}
return arr
}

const createHues = length => {
const hueStep = 360 / length
return base => {
const hues = createArray(length)
const hues = Array(length).fill()
.map((x, i) => i)
.map(n => Math.floor((base + (n * hueStep)) % 360))

return hues
Expand All @@ -41,8 +25,13 @@ const desat = n => hex => {
}

const createBlack = hex => {
const d = desat(1/8)(hex)
return chroma(d).luminance(.05).hex()
const d = desat(1 / 8)(hex)
return chroma(d).luminance(lums.slice(-1).pop()).hex()
}

const createWhite = hex => {
const d = desat(1 / 8)(hex)
return chroma(d).luminance(lums.slice().shift()).hex()
}

const createShades = hex => {
Expand All @@ -54,7 +43,7 @@ const createShades = hex => {
// Mappers
const keyword = hex => {
const [ hue, sat ] = chroma(hex).hsl()
if (sat < .5) {
if (sat < 0.5) {
return 'gray'
}
const name = hueName(hue)
Expand All @@ -77,20 +66,25 @@ const palx = (hex, options = {}) => {

colors.push({
key: 'black',
value: createBlack('' + color.hex())
value: createBlack('' + color.hex()),
})

colors.push({
key: 'white',
value: createWhite('' + color.hex()),
})

colors.push({
key: 'gray',
value: createShades(desat(1/8)('' + color.hex()))
value: createShades(desat(1 / 8)('' + color.hex())),
})

hues.forEach(h => {
const c = chroma.hsl(h, sat, lte)
const key = keyword(c)
colors.push({
key,
value: createShades('' + c.hex())
value: createShades('' + c.hex()),
})
})

Expand All @@ -102,4 +96,3 @@ const palx = (hex, options = {}) => {
}

module.exports = palx

2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ test('returns a gray object for desaturated base colors', t => {
const colors = palx('#444')
t.is(typeof colors, 'object')
t.snapshot(colors)
t.is(Object.keys(colors).length, 4)
t.is(Object.keys(colors).length, 5)
})
4 changes: 3 additions & 1 deletion test/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Generated by [AVA](https://ava.li).
'#007a70',
'#004842',
],
fuschia: [
fuchsia: [
'#fdf8fe',
'#f9e8fa',
'#f5d8f7',
Expand Down Expand Up @@ -155,6 +155,7 @@ Generated by [AVA](https://ava.li).
'#864adb',
'#4c00b8',
],
white: '#f9f9fa',
yellow: [
'#fbfaec',
'#f3efc4',
Expand Down Expand Up @@ -200,4 +201,5 @@ Generated by [AVA](https://ava.li).
'#6c6c6c',
'#3f3f3f',
],
white: '#f9f9f9',
}
Binary file modified test/snapshots/index.js.snap
Binary file not shown.