Skip to content

Commit

Permalink
Switch to internal color engine
Browse files Browse the repository at this point in the history
  • Loading branch information
arnelenero committed Apr 1, 2022
1 parent bd110e3 commit 437f12d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/color/colorString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import hslToRgb from './transforms/hslToRgb'
import type { HSL } from './hsl'
import type { RGB } from './rgb'

function roundRgb(rgb: RGB): RGB {
return {
r: Math.round(rgb.r),
g: Math.round(rgb.g),
b: Math.round(rgb.b),
a: rgb.a, // do not round off alpha
}
}

function rgbToHexString(rgb: RGB): string {
const int =
((Math.round(rgb.r) & 0xff) << 16) +
Expand All @@ -17,6 +26,7 @@ function rgbToHexString(rgb: RGB): string {
}

function rgbToRgbaString(rgb: RGB): string {
rgb = roundRgb(rgb)
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`
}

Expand Down
4 changes: 2 additions & 2 deletions src/mappings/__tests__/analogue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import analogue from '../analogue'
describe('analogue', () => {
it('returns a new hex color value with hue rotated in steps of 30˚', () => {
const color = analogue('blue', 1)
expect(color).toBe('#7F00FF')
expect(color).toBe('#8000FF')
})

it('rotates the hue in the opposite direction if key is negative', () => {
const color = analogue('blue', -1)
expect(color).toBe('#007FFF')
expect(color).toBe('#0080FF')
})

it('returns the base color if key is not a number', () => {
Expand Down
9 changes: 4 additions & 5 deletions src/mappings/lightness.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Color from 'color'

import normalize from '../normalize'
import colorString from '../color/colorString'
import hsl from '../color/hsl'

import type { ColorMapping } from '../palette'

Expand All @@ -16,11 +15,11 @@ import type { ColorMapping } from '../palette'
const lightness: ColorMapping = (baseColor, key) => {
if (typeof key !== 'number') return baseColor

const base = Color(baseColor)
const base = hsl(baseColor)

const targetL = Math.min(Math.max(key, 0), 100)

return normalize(base.lightness(targetL))
return colorString({ ...base, l: targetL })
}

export default lightness
9 changes: 4 additions & 5 deletions src/mappings/opacity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Color from 'color'

import normalize from '../normalize'
import colorString from '../color/colorString'
import rgb from '../color/rgb'

import type { ColorMapping } from '../palette'

Expand All @@ -16,11 +15,11 @@ import type { ColorMapping } from '../palette'
const opacity: ColorMapping = (baseColor, key) => {
if (typeof key !== 'number') return baseColor

const base = Color(baseColor)
const base = rgb(baseColor)

const targetA = Math.min(Math.max(key, 0), 1)

return normalize(base.alpha(targetA))
return colorString({ ...base, a: targetA })
}

export default opacity
11 changes: 5 additions & 6 deletions src/mappings/rotation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Color from 'color'

import normalize from '../normalize'
import colorString from '../color/colorString'
import hsl from '../color/hsl'

import type { ColorMapping } from '../palette'

Expand All @@ -18,11 +17,11 @@ import type { ColorMapping } from '../palette'
const rotation: ColorMapping = (baseColor, key) => {
if (typeof key !== 'number' || key === 0) return baseColor

const base = Color(baseColor)
const base = hsl(baseColor)

const targetH = (base.hue() + key) % 360
const targetH = (base.h + key) % 360

return normalize(base.hue(targetH))
return colorString({ ...base, h: targetH })
}

export default rotation
9 changes: 4 additions & 5 deletions src/mappings/saturation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Color from 'color'

import normalize from '../normalize'
import colorString from '../color/colorString'
import hsl from '../color/hsl'

import type { ColorMapping } from '../palette'

Expand All @@ -16,11 +15,11 @@ import type { ColorMapping } from '../palette'
const saturation: ColorMapping = (baseColor, key) => {
if (typeof key !== 'number') return baseColor

const base = Color(baseColor)
const base = hsl(baseColor)

const targetS = Math.min(Math.max(key, 0), 100)

return normalize(base.saturationl(targetS))
return colorString({ ...base, s: targetS })
}

export default saturation
4 changes: 2 additions & 2 deletions src/presets/__tests__/harmony.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('harmony', () => {
const baseColors = harmony('#0000FF')
expect(baseColors).toEqual({
primary: '#0000FF',
secondary: '#7F6C93',
secondary: '#806C93',
accent: '#E61980',
neutral: '#797986',
error: '#BB2211',
Expand All @@ -22,7 +22,7 @@ describe('harmony', () => {
expect(baseColors).toEqual({
primary: 'rgba(0, 0, 255, 0.5)',
secondary: 'rgba(128, 108, 147, 0.5)',
accent: 'rgba(230, 26, 128, 0.5)',
accent: 'rgba(230, 25, 128, 0.5)',
neutral: 'rgba(121, 121, 134, 0.5)',
error: '#BB2211',
})
Expand Down

0 comments on commit 437f12d

Please sign in to comment.