Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figma mix #699

Merged
merged 2 commits into from
Jul 21, 2023
Merged
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
36 changes: 36 additions & 0 deletions src/transformers/colorToRgbaFloat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ describe('Transformer: colorToRgbaFloat', () => {
])
})

it('transforms `color` tokens including mix', () => {
expect(
[
getMockToken({value: '#343434', mix: {color: '#000000', weight: 0.5}}),
getMockToken({value: '#34343466', mix: {color: '#000000'}}),
getMockToken({value: 'rgb(100,200,255)', mix: {weight: 0.5}}),
getMockToken({value: 'rgba(100,200,255,0.8)', mix: {color: undefined, weight: undefined}}),
].map(item => colorToRgbaFloat.transformer(item, {})),
).toStrictEqual([
{
b: 0.10196078431372549,
g: 0.10196078431372549,
r: 0.10196078431372549,
a: 1,
},
{
b: 0.050980392156862744,
g: 0.050980392156862744,
r: 0.050980392156862744,
a: 0.4,
},
{
r: 0.39215686274509803,
g: 0.7843137254901961,
b: 1,
a: 1,
},
{
r: 0.39215686274509803,
g: 0.7843137254901961,
b: 1,
a: 0.8,
},
])
})

it('it forwards `rgb float` values', () => {
const input = [
getMockToken({
Expand Down
9 changes: 5 additions & 4 deletions src/transformers/colorToRgbaFloat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {toHex} from 'color2k'
import {mix, toHex} from 'color2k'
import {isColor} from '~/src/filters'
import type StyleDictionary from 'style-dictionary'
import {getTokenValue} from './utilities/getTokenValue'

const toRgbaFloat = (color: string, alpha?: number) => {
const toRgbaFloat = (token: StyleDictionary.TransformedToken, alpha?: number) => {
// get hex value from color string
const hex = toHex(color)
// const hex = toHex(color)
const hex = toHex(mix(getTokenValue(token), token.mix?.color || getTokenValue(token), token.mix?.weight || 0))
// retrieve spots from hex value (hex 3, hex 6 or hex 8)
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex) ?? ['00', '00', '00']
// return parsed rgba float object using alpha value from token, from hex code or defaults to 1
Expand Down Expand Up @@ -47,6 +48,6 @@ export const colorToRgbaFloat: StyleDictionary.Transform = {
// skip if value is already rgb float
if (isRgbaFloat(token.value)) return token.value
// convert hex or rgb values to rgba float
return toRgbaFloat(getTokenValue(token), token.alpha)
return toRgbaFloat(token, token.alpha)
},
}
Loading