Skip to content

Commit

Permalink
Avoid converting hex colors to rgb if alpha=1
Browse files Browse the repository at this point in the history
  • Loading branch information
veej committed Dec 1, 2023
1 parent c5dd175 commit 2a87623
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
import { useConfiguratorStatusContext } from "../ConfiguratorStatusContext";
import { useTranslation } from "react-i18next";
import { ColorPickerField } from "../ColorPickerField/ColorPickerField";
import { ColorToken, colorTokenToRGBA } from "../utils/paletteUtils";
import { ColorToken, colorTokenToValue } from "../utils/paletteUtils";

export function ColorSelector(props: {
label: LocalizedString;
value: ColorToken;
onChange: (value: ColorToken) => void;
}) {
const colors = useConfiguratorStatusContext().theme.colors;
const selectedColor = colorTokenToRGBA(colors)(props.value);
const selectedColor = colorTokenToValue(colors)(props.value);
const { t } = useTranslation();

return (
Expand Down
4 changes: 2 additions & 2 deletions packages/configuration-builder/src/utils/colorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ export function HexToRGB(hex: HexColor): RGB {
}

export function withAlpha(color: HexColor, alpha: number) {
const rgb = HexToRGB(color);
if (alpha === 100) {
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
return color;
} else {
const rgb = HexToRGB(color);
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha / 100})`;
}
}
4 changes: 2 additions & 2 deletions packages/configuration-builder/src/utils/paletteUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ export type ColorToken = {
alpha: number;
};

export function colorTokenToRGBA(colors: ThemeConfig["colors"]) {
export function colorTokenToValue(colors: ThemeConfig["colors"]) {
return (colorToken: ColorToken): string | undefined => {
if (colorToken.colorKey === "black") {
return withAlpha("#000000" as HexColor, colorToken.alpha);
}
if (colorToken.colorKey === "white") {
return withAlpha("#FFFFFF" as HexColor, colorToken.alpha);
return withAlpha("#ffffff" as HexColor, colorToken.alpha);
}
const [paletteName, step] = colorToken.colorKey.split("-");
const keyColor = getPaletteKeyColor(paletteName as PaletteName, colors);
Expand Down
6 changes: 3 additions & 3 deletions packages/configuration-builder/src/utils/preview.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { BentoTheme } from "@buildo/bento-design-system";
import { useConfiguratorStatusContext } from "../ConfiguratorStatusContext";
import { ColorToken, colorTokenToRGBA as _colorTokenToRGBA } from "./paletteUtils";
import { ColorToken, colorTokenToValue as _colorTokenToValue } from "./paletteUtils";

export function useConfiguredTheme(): BentoTheme & object {
const { tokens: _tokens, colors } = useConfiguratorStatusContext().theme;
const tokens = _tokens as Record<string, Record<string, ColorToken>>;

const colorTokenToRGBA = _colorTokenToRGBA(colors);
const colorTokenToValue = _colorTokenToValue(colors);

const theme: BentoTheme & object = Object.keys(tokens).reduce(
(acc, key) => ({
...acc,
[key]: Object.keys(tokens[key]).reduce(
(acc2, key2) => ({
...acc2,
[key2]: colorTokenToRGBA(tokens[key][key2]),
[key2]: colorTokenToValue(tokens[key][key2]),
}),
{}
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useConfiguratorStatusContext } from "../ConfiguratorStatusContext";
import { ColorToken, colorTokenToRGBA as _colorTokenToRGBA } from "./paletteUtils";
import { ColorToken, colorTokenToValue as _colorTokenToValue } from "./paletteUtils";

function colorTokenToVarName(colorToken: ColorToken): string {
const tokenPart = `${colorToken.colorKey.replace("-", "")}`;
Expand All @@ -11,14 +11,14 @@ function colorTokenToVarName(colorToken: ColorToken): string {

export function useConfigurationExporter(): () => string {
const { tokens, colors } = useConfiguratorStatusContext().theme;
const colorTokenToRGBA = _colorTokenToRGBA(colors);
const colorTokenToValue = _colorTokenToValue(colors);
return () => {
const prelude = `import { BentoTheme } from "@buildo/bento-design-system";`;

const usedColors: Record<string, string> = {};
Object.entries(tokens).forEach(([_, tokensSection]) => {
Object.entries(tokensSection).forEach(([_, colorToken]) => {
const rgba = colorTokenToRGBA(colorToken);
const rgba = colorTokenToValue(colorToken);
if (rgba) {
usedColors[colorTokenToVarName(colorToken)] = rgba;
}
Expand Down

0 comments on commit 2a87623

Please sign in to comment.