Skip to content

Commit 2d5cdc2

Browse files
author
Dario Soller
committed
fix: resolve calculated color modifier values [348]
1 parent 1c151ee commit 2d5cdc2

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/checkAndEvaluateMath.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ function splitMultiIntoSingleValues(expr: string): string[] {
7575
return [expr];
7676
}
7777

78-
function parseAndReduce(expr: string, fractionDigits = defaultFractionDigits): string | number {
78+
export function parseAndReduce(
79+
expr: string,
80+
fractionDigits = defaultFractionDigits,
81+
): string | number {
7982
let result: string | number = expr;
8083

8184
// Check if expression is already a number

src/color-modifiers/modifyColor.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { mix } from './mix.js';
44
import { darken } from './darken.js';
55
import { lighten } from './lighten.js';
66
import { ColorModifier } from '@tokens-studio/types';
7+
import { parseAndReduce } from '../checkAndEvaluateMath.js';
78

89
// Users using UIColor swift format are blocked from using such transform in
910
// combination with this color modify transform when using references.
@@ -39,24 +40,25 @@ export function modifyColor(
3940

4041
const color = new Color(baseColor);
4142
let returnedColor = color;
43+
const modifyValueResolvedCalc = Number(parseAndReduce(modifier.value, 4));
4244
try {
4345
switch (modifier.type) {
4446
case 'lighten':
45-
returnedColor = lighten(color, modifier.space, Number(modifier.value));
47+
returnedColor = lighten(color, modifier.space, modifyValueResolvedCalc);
4648
break;
4749
case 'darken':
48-
returnedColor = darken(color, modifier.space, Number(modifier.value));
50+
returnedColor = darken(color, modifier.space, modifyValueResolvedCalc);
4951
break;
5052
case 'mix':
5153
returnedColor = mix(
5254
color,
5355
modifier.space,
54-
Number(modifier.value),
56+
modifyValueResolvedCalc,
5557
new Color(modifier.color),
5658
);
5759
break;
5860
case 'alpha': {
59-
returnedColor = transparentize(color, Number(modifier.value));
61+
returnedColor = transparentize(color, modifyValueResolvedCalc);
6062
break;
6163
}
6264
default:
@@ -75,11 +77,7 @@ export function modifyColor(
7577
}
7678
}
7779

78-
return returnedColor.toString({
79-
inGamut: true,
80-
precision: 3,
81-
format: modifier.format,
82-
});
80+
return returnedColor.toString({ inGamut: true, precision: 3, format: modifier.format });
8381
} catch (e) {
8482
return baseColor;
8583
}

0 commit comments

Comments
 (0)