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

Add support for HEX8 in HEXRGBa transform #341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add support for HEX8 in HEXRGBa transform #341

wants to merge 1 commit into from

Conversation

mck
Copy link

@mck mck commented Feb 24, 2025

Optimizes the transformHEXRGBaForCSS function to better handle hex colors with alpha channels:

  • Only transforms hex colors with alpha (#RGBA, #RRGGBBAA) to rgba format
  • Preserves standard hex colors (#RGB, #RRGGBB) in their original format
  • Maintains support for hex colors within rgba() functions

@mck mck requested a review from jorenbroekema February 24, 2025 19:34
Copy link

changeset-bot bot commented Feb 24, 2025

🦋 Changeset detected

Latest commit: 6ae7729

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tokens-studio/sd-transforms Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mck mck self-assigned this Feb 24, 2025
@@ -2,7 +2,7 @@ import Color from 'colorjs.io';
import { DesignToken } from 'style-dictionary/types';

/**
* Helper: Transforms hex rgba colors used in figma tokens:
* Helper: Transforms hex to rgba colors used in figma tokens:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Helper: Transforms hex to rgba colors used in figma tokens:
* Helper: Transforms `rgba(hex, alpha)` format which is used in figma tokens, to valid CSS rgba colors:

Comment on lines +16 to +20
// Fast path for invalid hex
if (hex.length < 4) return hex;

// Determine format based on length
const hexLength = hex.length - 1; // subtract 1 for #
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Fast path for invalid hex
if (hex.length < 4) return hex;
// Determine format based on length
const hexLength = hex.length - 1; // subtract 1 for #
// Determine format based on length
const hexLength = hex.length - 1; // subtract 1 for #
// Fast path for invalid hex
if (hexLength < 3) return hex;

This makes more sense, otherwise as a reader of the code I'd be wondering "what about hex3, 3 is less than 4"

Comment on lines +22 to +39
// Only transform hex colors with alpha channel
const hasAlpha = hexLength === 4 || hexLength === 8;
if (!hasAlpha) return hex;

let hexColor = hex;
let alpha = '1';

// Convert shorthand to full format if necessary
if (hexLength === 4) {
const r = hex[1],
g = hex[2],
b = hex[3],
a = hex[4];
hexColor = `#${r}${r}${g}${g}${b}${b}`;
alpha = (parseInt(a + a, 16) / 255).toString();
} else if (hexLength === 8) {
alpha = (parseInt(hex.slice(7), 16) / 255).toString();
hexColor = hex.slice(0, 7);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deleted entirely, as Colorjs.io can parse hex3/4/6/8 just fine.

Comment on lines +42 to +43
const [r, g, b] = new Color(hexColor).srgb;
return `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${alpha})`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [r, g, b] = new Color(hexColor).srgb;
return `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${alpha})`;
const parsed = new Color(hexColor);
const [r, g, b] = parsed.srgb;
const alpha = parsed.alpha;
return `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${alpha})`;

Comment on lines +51 to +53
if (val.startsWith('#')) {
return transformHexColor(val);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be deleted right? If it's already hex format, there's no point in transforming it to rgba()?

Comment on lines +60 to +61
const [r, g, b] = new Color(hex).srgb;
return `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${alpha})`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about rgba(hex8, alpha) or rgba(hex4, alpha) so basically which alpha do we take if they are both defined in the hex but also in the alpha component of the expression?

Comment on lines +112 to +113
expect(transformHEXRGBaForCSS({ value: '#F00F' })).to.equal('rgba(255, 0, 0, 1)');
expect(transformHEXRGBaForCSS({ value: '#F000' })).to.equal('rgba(255, 0, 0, 0)');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect these to have the same output as input, since it's valid hex format.

Comment on lines +129 to +130
expect(transformHEXRGBaForCSS({ value: '#000000FF' })).to.equal('rgba(0, 0, 0, 1)');
expect(transformHEXRGBaForCSS({ value: '#00000000' })).to.equal('rgba(0, 0, 0, 0)');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, so far this transform only is there to handle rgba(hex, alpha), not transform hex4/8 to rgba. I'm not entirely against repurposing this transform to also do this, but it would definitely be a significant breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants