Skip to content

Commit

Permalink
fix lint...
Browse files Browse the repository at this point in the history
  • Loading branch information
rpearce committed Sep 9, 2019
1 parent 26aaf26 commit 3f75f94
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 84 deletions.
4 changes: 1 addition & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const presets = [
'@babel/preset-env'
]
const presets = ['@babel/preset-env']

module.exports = {
presets
Expand Down
6 changes: 2 additions & 4 deletions source/blend.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Forumula source: https://stackoverflow.com/a/29321264/680394

// blend :: (Integer, Integer, Number) -> Integer
const blend = (x, y, n=0) =>
Math.round(
Math.sqrt((1 - n) * Math.pow(x, 2) + n * Math.pow(y, 2))
)
const blend = (x, y, n = 0) =>
Math.round(Math.sqrt((1 - n) * Math.pow(x, 2) + n * Math.pow(y, 2)))

export default blend
3 changes: 1 addition & 2 deletions source/blendAlpha.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Formula source: https://stackoverflow.com/a/29321264/680394

// blendAlpha :: (Integer, Integer, Number) -> Integer
const blendAlpha = (x, y, n=0) =>
Number(((1 - n) * x + n * y).toFixed(2))
const blendAlpha = (x, y, n = 0) => Number(((1 - n) * x + n * y).toFixed(2))

export default blendAlpha
8 changes: 3 additions & 5 deletions source/hexBlend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import rgbaToHex from './rgbaToHex'

// hexBlend :: (String Hex, String Hex, Number) -> String Hex
const hexBlend = (x, y, n) => {
const [ rX, gX, bX, aX ] = hexToRgba(x)
const [ rY, gY, bY, aY ] = hexToRgba(y)
const [rX, gX, bX, aX] = hexToRgba(x)
const [rY, gY, bY, aY] = hexToRgba(y)
const hex = rgbaToHex([
blend(rX, rY, n),
blend(gX, gY, n),
blend(bX, bY, n),
blendAlpha(aX, aY, n)
])

return aX === 1 && aY === 1
? hex.slice(0, 7)
: hex
return aX === 1 && aY === 1 ? hex.slice(0, 7) : hex
}

export default hexBlend
10 changes: 2 additions & 8 deletions source/hexToRgba.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import parse from './parse'

const getAlpha = x =>
x ? parseFloat((parseInt(x, 16) / 0xff).toFixed(2)) : 1
const getAlpha = x => (x ? parseFloat((parseInt(x, 16) / 0xff).toFixed(2)) : 1)

// hexToRgba :: String -> [ Integer, Integer, Integer, Number ]
const hexToRgba = x => {
const hex = parse(x)
const head = hex.slice(0, 6)
const tail = hex.slice(6)
const n = parseInt(head, 16)
return [
n >> 16 & 0xff,
n >> 8 & 0xff,
n >> 0 & 0xff,
getAlpha(tail)
]
return [(n >> 16) & 0xff, (n >> 8) & 0xff, (n >> 0) & 0xff, getAlpha(tail)]
}

export default hexToRgba
4 changes: 2 additions & 2 deletions source/hexToRgbaCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import rgbaToRgbaCss from './rgbaToRgbaCss'

// hexToRgbaCss :: (String, Number) -> String Rgba
const hexToRgbaCss = (str, alpha) => {
const [ r, g, b, _a ] = hexToRgba(str)
const [r, g, b, _a] = hexToRgba(str)
const a = alpha || _a
return rgbaToRgbaCss([ r, g, b, a ])
return rgbaToRgbaCss([r, g, b, a])
}

export default hexToRgbaCss
5 changes: 2 additions & 3 deletions source/parse.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// repeat :: String HexShorthand -> String Hex
const repeat = x =>
[ ...x ].map(y => y + y).join('')
const repeat = x => [...x].map(y => y + y).join('')

// parse :: String -> String Hex
const parse = (x='') => {
const parse = (x = '') => {
const hex = x.replace(/^#/, '')
const len = hex.length
return len === 3 || len === 4 ? repeat(hex) : hex
Expand Down
11 changes: 3 additions & 8 deletions source/parseRgba.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const regex = /[0-9.]+/gi

// parseRgba :: String Rgba -> [ Integer, Integer, Integer, Number ]
const parseRgba = (x='') => {
const [ r=0, g=0, b=0, a=1 ] = x.match(regex) || []
return [
parseInt(r, 10),
parseInt(g, 10),
parseInt(b, 10),
parseFloat(a)
]
const parseRgba = (x = '') => {
const [r = 0, g = 0, b = 0, a = 1] = x.match(regex) || []
return [parseInt(r, 10), parseInt(g, 10), parseInt(b, 10), parseFloat(a)]
}

export default parseRgba
4 changes: 2 additions & 2 deletions source/rgbaCssBlend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import rgbaToRgbaCss from './rgbaToRgbaCss'

// rgbaCssBlend :: (String Rgba, String Rgba, Number) -> String Rgba
const rgbaCssBlend = (x, y, n) => {
const [ rX, gX, bX, aX ] = parseRgba(x)
const [ rY, gY, bY, aY ] = parseRgba(y)
const [rX, gX, bX, aX] = parseRgba(x)
const [rY, gY, bY, aY] = parseRgba(y)

return rgbaToRgbaCss([
blend(rX, rY, n),
Expand Down
7 changes: 5 additions & 2 deletions source/rgbaToHex.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// rgbaToHex :: [ Integer, Integer, Integer, Number ] -> String Hex
const rgbaToHex = ([ r=0, g=0, b=0, a=1 ]=[]) => {
const rgbaToHex = ([r = 0, g = 0, b = 0, a = 1] = []) => {
const rgbN = (1 << 24) | (r << 16) | (g << 8) | b
const hex = rgbN.toString(16).slice(1)
const alpha = (a * 255).toString(16).padStart(2, '0').slice(0, 2)
const alpha = (a * 255)
.toString(16)
.padStart(2, '0')
.slice(0, 2)
return `#${hex}${alpha}`
}

Expand Down
2 changes: 1 addition & 1 deletion source/rgbaToRgbaCss.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rgbaToRgbaCss :: [ Integer, Integer, Integer, Number ] -> String Rgba
const rgbaToRgbaCss = ([ r=0, g=0, b=0, a=1 ]=[]) =>
const rgbaToRgbaCss = ([r = 0, g = 0, b = 0, a = 1] = []) =>
`rgba(${r},${g},${b},${a})`

export default rgbaToRgbaCss
48 changes: 24 additions & 24 deletions test/hexToRgba.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@ import test from 'ava'
import { hexToRgba } from '../source'

test('pure hex-8', t => {
t.deepEqual(hexToRgba('0099ff80'), [ 0, 153, 255, 0.5 ])
t.deepEqual(hexToRgba('00000027'), [ 0, 0, 0, 0.15 ])
t.deepEqual(hexToRgba('FFFFFF88'), [ 255, 255, 255, 0.53 ])
t.deepEqual(hexToRgba('0099ff80'), [0, 153, 255, 0.5])
t.deepEqual(hexToRgba('00000027'), [0, 0, 0, 0.15])
t.deepEqual(hexToRgba('FFFFFF88'), [255, 255, 255, 0.53])
})

test('prefixed hex-8', t => {
t.deepEqual(hexToRgba('#0099ff80'), [ 0, 153, 255, 0.5 ])
t.deepEqual(hexToRgba('#00000027'), [ 0, 0, 0, 0.15 ])
t.deepEqual(hexToRgba('#FFFFFF88'), [ 255, 255, 255, 0.53 ])
t.deepEqual(hexToRgba('#0099ff80'), [0, 153, 255, 0.5])
t.deepEqual(hexToRgba('#00000027'), [0, 0, 0, 0.15])
t.deepEqual(hexToRgba('#FFFFFF88'), [255, 255, 255, 0.53])
})

test('pure hex-6', t => {
t.deepEqual(hexToRgba('0099ff'), [ 0, 153, 255, 1 ])
t.deepEqual(hexToRgba('000000'), [ 0, 0, 0, 1 ])
t.deepEqual(hexToRgba('FFFFFF'), [ 255, 255, 255, 1 ])
t.deepEqual(hexToRgba('0099ff'), [0, 153, 255, 1])
t.deepEqual(hexToRgba('000000'), [0, 0, 0, 1])
t.deepEqual(hexToRgba('FFFFFF'), [255, 255, 255, 1])
})

test('prefixed hex-6', t => {
t.deepEqual(hexToRgba('#0099ff'), [ 0, 153, 255, 1 ])
t.deepEqual(hexToRgba('#000000'), [ 0, 0, 0, 1 ])
t.deepEqual(hexToRgba('#FFFFFF'), [ 255, 255, 255, 1 ])
t.deepEqual(hexToRgba('#0099ff'), [0, 153, 255, 1])
t.deepEqual(hexToRgba('#000000'), [0, 0, 0, 1])
t.deepEqual(hexToRgba('#FFFFFF'), [255, 255, 255, 1])
})

test('pure hex-4', t => {
t.deepEqual(hexToRgba('09f9'), [ 0, 153, 255, 0.6 ])
t.deepEqual(hexToRgba('FFF9'), [ 255, 255, 255, 0.6 ])
t.deepEqual(hexToRgba('0009'), [ 0, 0, 0, 0.6 ])
t.deepEqual(hexToRgba('09f9'), [0, 153, 255, 0.6])
t.deepEqual(hexToRgba('FFF9'), [255, 255, 255, 0.6])
t.deepEqual(hexToRgba('0009'), [0, 0, 0, 0.6])
})

test('prefixed hex-4', t => {
t.deepEqual(hexToRgba('#09f9'), [ 0, 153, 255, 0.6 ])
t.deepEqual(hexToRgba('#FFF9'), [ 255, 255, 255, 0.6 ])
t.deepEqual(hexToRgba('#0009'), [ 0, 0, 0, 0.6 ])
t.deepEqual(hexToRgba('#09f9'), [0, 153, 255, 0.6])
t.deepEqual(hexToRgba('#FFF9'), [255, 255, 255, 0.6])
t.deepEqual(hexToRgba('#0009'), [0, 0, 0, 0.6])
})

test('pure hex-3', t => {
t.deepEqual(hexToRgba('09f'), [ 0, 153, 255, 1 ])
t.deepEqual(hexToRgba('FFF'), [ 255, 255, 255, 1 ])
t.deepEqual(hexToRgba('000'), [ 0, 0, 0, 1 ])
t.deepEqual(hexToRgba('09f'), [0, 153, 255, 1])
t.deepEqual(hexToRgba('FFF'), [255, 255, 255, 1])
t.deepEqual(hexToRgba('000'), [0, 0, 0, 1])
})

test('prefixed hex-3', t => {
t.deepEqual(hexToRgba('#09f'), [ 0, 153, 255, 1 ])
t.deepEqual(hexToRgba('#FFF'), [ 255, 255, 255, 1 ])
t.deepEqual(hexToRgba('#000'), [ 0, 0, 0, 1 ])
t.deepEqual(hexToRgba('#09f'), [0, 153, 255, 1])
t.deepEqual(hexToRgba('#FFF'), [255, 255, 255, 1])
t.deepEqual(hexToRgba('#000'), [0, 0, 0, 1])
})
14 changes: 7 additions & 7 deletions test/parseRgba.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import test from 'ava'
import { parseRgba } from '../source'

test('undefined', t => {
t.deepEqual(parseRgba(), [ 0, 0, 0, 1 ])
t.deepEqual(parseRgba(), [0, 0, 0, 1])
})

test('empty string', t => {
t.deepEqual(parseRgba(''), [ 0, 0, 0, 1 ])
t.deepEqual(parseRgba(''), [0, 0, 0, 1])
})

test('purple – no alpha', t => {
t.deepEqual(parseRgba('rgb(120, 55, 168)'), [ 120, 55, 168, 1 ])
t.deepEqual(parseRgba('rgb(120, 55, 168)'), [120, 55, 168, 1])
})

test('purple – alpha is 1', t => {
t.deepEqual(parseRgba('rgba(120, 55, 168, 1)'), [ 120, 55, 168, 1 ])
t.deepEqual(parseRgba('rgba(120, 55, 168, 1)'), [120, 55, 168, 1])
})

test('purple – alpha is 0.3', t => {
t.deepEqual(parseRgba('rgba(120, 55, 168, 0.3)'), [ 120, 55, 168, 0.3 ])
t.deepEqual(parseRgba('rgba(120, 55, 168, 0.3)'), [120, 55, 168, 0.3])
})

test('white', t => {
t.deepEqual(parseRgba('rgba(255, 255, 255, 1)'), [ 255, 255, 255, 1 ])
t.deepEqual(parseRgba('rgba(255, 255, 255, 1)'), [255, 255, 255, 1])
})

test('black', t => {
t.deepEqual(parseRgba('rgba(0, 0, 0, 1)'), [ 0, 0, 0, 1 ])
t.deepEqual(parseRgba('rgba(0, 0, 0, 1)'), [0, 0, 0, 1])
})
30 changes: 24 additions & 6 deletions test/rgbaCssBlend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,43 @@ import test from 'ava'
import { rgbaCssBlend } from '../source'

test('purple to white – point is 0.7', t => {
t.is(rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(255, 255, 255, 1)', 0.7), 'rgba(223,215,232,1)')
t.is(
rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(255, 255, 255, 1)', 0.7),
'rgba(223,215,232,1)'
)
})

test('purple to black – point is 0.7', t => {
t.is(rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(0, 0, 0, 1)', 0.7), 'rgba(66,30,92,1)')
t.is(
rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(0, 0, 0, 1)', 0.7),
'rgba(66,30,92,1)'
)
})

test('orange to white – point is 0.7', t => {
t.is(rgbaCssBlend('rgba(255, 102, 2, 1)', 'rgba(255, 255, 255, 1)', 0.7), 'rgba(255,221,213,1)')
t.is(
rgbaCssBlend('rgba(255, 102, 2, 1)', 'rgba(255, 255, 255, 1)', 0.7),
'rgba(255,221,213,1)'
)
})

test('orange to black – point is 0.7', t => {
t.is(rgbaCssBlend('rgba(255, 102, 2, 1)', 'rgba(0, 0, 0, 1)', 0.7), 'rgba(140,56,1,1)')
t.is(
rgbaCssBlend('rgba(255, 102, 2, 1)', 'rgba(0, 0, 0, 1)', 0.7),
'rgba(140,56,1,1)'
)
})

test('purple to orange – point is 0.5', t => {
t.is(rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(255, 102, 2, 1)', 0.5), 'rgba(199,82,119,1)')
t.is(
rgbaCssBlend('rgba(120, 55, 168, 1)', 'rgba(255, 102, 2, 1)', 0.5),
'rgba(199,82,119,1)'
)
})

test('purple to white – point is 0.7 – half opacity', t => {
t.is(rgbaCssBlend('rgba(120, 55, 168, 0.5)', 'rgba(255, 255, 255, 1)', 0.7), 'rgba(223,215,232,0.85)')
t.is(
rgbaCssBlend('rgba(120, 55, 168, 0.5)', 'rgba(255, 255, 255, 1)', 0.7),
'rgba(223,215,232,0.85)'
)
})
8 changes: 4 additions & 4 deletions test/rgbaToHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ test('empty array', t => {
})

test('purple – no alpha', t => {
t.is(rgbaToHex([ 120, 55, 168 ]), '#7837a8ff')
t.is(rgbaToHex([120, 55, 168]), '#7837a8ff')
})

test('purple – alpha is 1', t => {
t.is(rgbaToHex([ 120, 55, 168, 1 ]), '#7837a8ff')
t.is(rgbaToHex([120, 55, 168, 1]), '#7837a8ff')
})

test('purple – alpha is 0.5', t => {
t.is(rgbaToHex([ 120, 55, 168, 0.5 ]), '#7837a87f')
t.is(rgbaToHex([120, 55, 168, 0.5]), '#7837a87f')
})

test('purple – alpha is 0', t => {
t.is(rgbaToHex([ 120, 55, 168, 0 ]), '#7837a800')
t.is(rgbaToHex([120, 55, 168, 0]), '#7837a800')
})
6 changes: 3 additions & 3 deletions test/rgbaToRgbaCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ test('empty array', t => {
})

test('no alpha', t => {
t.is(rgbaToRgbaCss([ 120, 55, 168 ]), 'rgba(120,55,168,1)')
t.is(rgbaToRgbaCss([120, 55, 168]), 'rgba(120,55,168,1)')
})

test('alpha is 1', t => {
t.is(rgbaToRgbaCss([ 120, 55, 168, 1 ]), 'rgba(120,55,168,1)')
t.is(rgbaToRgbaCss([120, 55, 168, 1]), 'rgba(120,55,168,1)')
})

test('alpha is 0.3', t => {
t.is(rgbaToRgbaCss([ 120, 55, 168, 0.3 ]), 'rgba(120,55,168,0.3)')
t.is(rgbaToRgbaCss([120, 55, 168, 0.3]), 'rgba(120,55,168,0.3)')
})

0 comments on commit 3f75f94

Please sign in to comment.