Skip to content

Commit

Permalink
extract isRgbaFloat (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Feb 23, 2024
1 parent 3dd59fc commit 0394f6d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
18 changes: 1 addition & 17 deletions src/transformers/colorToRgbaFloat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {getTokenValue} from './utilities/getTokenValue'
import {rgbaFloatToHex} from './utilities/rgbaFloatToHex'
import mix from './utilities/mix'
import {hexToRgbaFloat} from './utilities/hexToRgbaFloat'
import {isRgbaFloat} from './utilities/isRgbaFloat'

const toRgbaFloat = (token: StyleDictionary.TransformedToken, alpha?: number) => {
let tokenValue = getTokenValue(token)
Expand All @@ -25,23 +26,6 @@ const toRgbaFloat = (token: StyleDictionary.TransformedToken, alpha?: number) =>
return hexToRgbaFloat(hex, alpha)
}

// sum up the values of all values in an array
const sum = (array: unknown[]): number => array.reduce((acc: number, v: unknown) => acc + parseInt(`${v}`), 0)

const isRgbaFloat = (value: unknown) => {
if (
value &&
typeof value === `object` &&
'r' in value &&
'g' in value &&
'b' in value &&
sum([value.r, value.g, value.b]) < 5
) {
return true
}
return false
}

/**
* @description converts color tokens rgba float with values from 0 - 1
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
Expand Down
7 changes: 1 addition & 6 deletions src/transformers/utilities/hexToRgbaFloat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
type RgbaFloat = {
r: number
g: number
b: number
a?: number
}
import type {RgbaFloat} from './isRgbaFloat'

export const hexToRgbaFloat = (hex: string, alpha?: number): RgbaFloat => {
// retrieve spots from hex value (hex 3, hex 4, hex 6 or hex 8)
Expand Down
23 changes: 23 additions & 0 deletions src/transformers/utilities/isRgbaFloat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type RgbaFloat = {
r: number
g: number
b: number
a?: number
}

// sum up the values of all values in an array
const sum = (array: unknown[]): number => array.reduce((acc: number, v: unknown) => acc + parseInt(`${v}`), 0)
// check if a value is an rgba float object
export const isRgbaFloat = (value: unknown): value is RgbaFloat => {
if (
value &&
typeof value === `object` &&
'r' in value &&
'g' in value &&
'b' in value &&
sum([value.r, value.g, value.b]) < 5
) {
return true
}
return false
}

0 comments on commit 0394f6d

Please sign in to comment.