Skip to content

Commit

Permalink
[#136] Add support for RGBA color values
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Dec 1, 2021
1 parent 199774d commit 963d2d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/token.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const R_NUMBER_COMPONENT = /(\d|-|\.)/
const R_FORMAT_CHUNKS = /([^\-0-9.]+)/g
const R_UNFORMATTED_VALUES = /[0-9.-]+/g
const R_RGB = (() => {
const R_RGBA = (() => {
const number = R_UNFORMATTED_VALUES.source
const comma = /,\s*/.source

return new RegExp(`rgb\\(${number}${comma}${number}${comma}${number}\\)`, 'g')
return new RegExp(
`rgba?\\(${number}${comma}${number}${comma}${number}(${comma}${number})?\\)`,
'g'
)
})()
const R_RGB_PREFIX = /^.*\(/
const R_RGBA_PREFIX = /^.*\(/
const R_HEX = /#([0-9]|[a-f]){3,6}/gi
const VALUE_PLACEHOLDER = 'VAL'

Expand Down Expand Up @@ -162,11 +165,16 @@ const sanitizeObjectForHexProps = stateObject => {
* @return {string}
* @private
*/
const sanitizeRGBChunk = rgbChunk => {
const numbers = rgbChunk.match(R_UNFORMATTED_VALUES).map(Math.floor)
const prefix = rgbChunk.match(R_RGB_PREFIX)[0]

return `${prefix}${numbers.join(',')})`
const sanitizeRGBAChunk = rgbChunk => {
const rgbaRawValues = rgbChunk.match(R_UNFORMATTED_VALUES)
const rgbNumbers = rgbaRawValues.slice(0, 3).map(Math.floor)
const prefix = rgbChunk.match(R_RGBA_PREFIX)[0]

if (rgbaRawValues.length === 3) {
return `${prefix}${rgbNumbers.join(',')})`
} else if (rgbaRawValues.length === 4) {
return `${prefix}${rgbNumbers.join(',')},${rgbaRawValues[3]})`
}
}

/**
Expand All @@ -178,7 +186,7 @@ const sanitizeRGBChunk = rgbChunk => {
* @private
*/
const sanitizeRGBChunks = formattedString =>
filterStringChunks(R_RGB, formattedString, sanitizeRGBChunk)
filterStringChunks(R_RGBA, formattedString, sanitizeRGBAChunk)

/**
* Note: It's the duty of the caller to convert the Array elements of the
Expand Down
15 changes: 14 additions & 1 deletion src/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ test('can tween an rgb color with a number in the tween', () => {
expect(interpolated.color).toEqual(to.color)
})

test('can tween an rgba color with a number in the tween', () => {
const from = { color: 'rgba(0,128,255,0)', x: 0 },
to = { color: 'rgba(128,255,0,1)', x: 10 }

let interpolated = interpolate(from, to, 0)
expect(interpolated.color).toEqual(from.color)
interpolated = interpolate(from, to, 0.5)
expect(interpolated.color).toEqual('rgba(64,191,127,0.5)')
expect(interpolated.x).toEqual(5)
interpolated = interpolate(from, to, 1)
expect(interpolated.color).toEqual(to.color)
})

test('can tween hex color values', () => {
const from = { color: '#ff00ff' },
to = { color: '#00ff00' }
Expand All @@ -49,7 +62,7 @@ test('can tween multiple rgb color tokens', () => {
expect(interpolated.color).toEqual(to.color)
})

test("each token chunk can have it's own easing curve", () => {
test('each token chunk can have its own easing curve', () => {
const from = { color: 'rgb(0,0,0)' },
to = { color: 'rgb(255,255,255)' },
easing = 'linear easeInQuad easeInCubic'
Expand Down

0 comments on commit 963d2d3

Please sign in to comment.