Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: support modern color spaces in color inputs (#5465)
Bogdan Chadkin21bce6105245
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.test.ts b/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.test.tsindex 52b09e8cec..4e96accdb2 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.test.ts+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.test.ts@@ -228,7 +228,6 @@ describe("convertGradientToTarget", () => {conic: "conic-gradient(red 0%, blue 100%)",radial: "radial-gradient(circle, red 0%, blue 100%)",};- const expectedColors = ["rgb(255 0 0 / 1)", "rgb(0 0 255 / 1)"];const gradientTypes: GradientType[] = ["linear", "conic", "radial"];const getStopColors = (stops: GradientStop[]) =>@@ -239,6 +238,8 @@ describe("convertGradientToTarget", () => {).map((color) => toValue(color));+ const expectedColorValues = ["rgb(255 0 0 / 1)", "rgb(0 0 255 / 1)"];+gradientTypes.forEach((sourceType) => {gradientTypes.forEach((targetType) => {test(`converts ${sourceType} gradient to ${targetType}`, () => {@@ -248,8 +249,8 @@ describe("convertGradientToTarget", () => {};const converted = convertGradientToTarget(styleValue, targetType);expect(converted.type).toBe(targetType);- expect(converted.stops).toHaveLength(expectedColors.length);- expect(getStopColors(converted.stops)).toEqual(expectedColors);+ expect(converted.stops).toHaveLength(expectedColorValues.length);+ expect(getStopColors(converted.stops)).toEqual(expectedColorValues);});});});@@ -541,17 +542,15 @@ describe("ensureGradientHasStops", () => {const result = ensureGradientHasStops(gradient);expect(result.stops).toHaveLength(2);expect(result.stops[0]?.color).toEqual({- type: "rgb",- r: 0,- g: 0,- b: 0,+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 0],alpha: 1,});expect(result.stops[1]?.color).toEqual({- type: "rgb",- r: 0,- g: 0,- b: 0,+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 0],alpha: 0,});});@@ -568,10 +567,9 @@ describe("ensureGradientHasStops", () => {const result = ensureGradientHasStops(gradient);expect(result.stops[0]?.color).toEqual({- type: "rgb",- r: 0,- g: 0,- b: 0,+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 0],alpha: 1,});});@@ -639,10 +637,9 @@ describe("styleValueToColor", () => {value: "#0000ff",};expect(styleValueToColor(style)).toEqual({- type: "rgb",- r: 0,- g: 0,- b: 255,+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],alpha: 1,});});@@ -658,10 +655,9 @@ describe("styleValueToColor", () => {test("parses invalid style when value is valid color string", () => {const style: StyleValue = { type: "invalid", value: "rgb(10 20 30)" };expect(styleValueToColor(style)).toEqual({- type: "rgb",- r: 10,- g: 20,- b: 30,+ type: "color",+ colorSpace: "srgb",+ components: [0.0392, 0.0784, 0.1176],alpha: 1,});});@@ -677,10 +673,9 @@ describe("styleValueToColor", () => {value: "hsl(180 100% 50%)",};expect(styleValueToColor(style)).toEqual({- type: "rgb",- r: 0,- g: 255,- b: 255,+ type: "color",+ colorSpace: "hsl",+ components: [180, 100, 50],alpha: 1,});});diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.ts b/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.tsindex 655b924c0b..4082bd1448 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.ts+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-utils.ts@@ -1,9 +1,9 @@import { clamp } from "@react-aria/utils";import {+ ColorValue,toValue,type CssProperty,type KeywordValue,- type RgbValue,type StyleValue,type Unit,type UnitValue,@@ -233,21 +233,27 @@ const toPercentUnitValue = (value: UnitValue): PercentUnitValue | undefined => {} satisfies PercentUnitValue;};-export const fallbackStopColor: RgbValue = {- type: "rgb",- r: 0,- g: 0,- b: 0,+export const fallbackStopColor: ColorValue = {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 0],alpha: 1,};+const transparentColor: ColorValue = {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 0],+ alpha: 0,+};+export const createDefaultStops = (): GradientStop[] => [{- color: { type: "rgb", r: 0, g: 0, b: 0, alpha: 1 },+ color: fallbackStopColor,position: { type: "unit", unit: "%", value: 0 },},{- color: { type: "rgb", r: 0, g: 0, b: 0, alpha: 0 },+ color: transparentColor,position: { type: "unit", unit: "%", value: 100 },},];@@ -1125,6 +1131,7 @@ export const updateGradientStop = <T extends ParsedGradient>(const parseColorString = (value: string): GradientStop["color"] | undefined => {const parsed = parseCssValue("color", value);if (+ parsed.type === "color" ||parsed.type === "rgb" ||parsed.type === "keyword" ||parsed.type === "var"@@ -1144,7 +1151,7 @@ export const styleValueToColor = (return parseColorString(styleValue.value);}- if (styleValue.type === "rgb") {+ if (styleValue.type === "rgb" || styleValue.type === "color") {return styleValue;}diff --git a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsxindex 9fb9936257..322fb59f88 100644--- a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx@@ -77,6 +77,7 @@ export const ColorPickerControl = ({}if (styleValue.type === "rgb" ||+ styleValue.type === "color" ||styleValue.type === "keyword" ||styleValue.type === "var" ||styleValue.type === "invalid"@@ -101,6 +102,7 @@ export const ColorPickerControl = ({onChangeComplete={({ value }) => {if (value.type === "rgb" ||+ value.type === "color" ||value.type === "keyword" ||value.type === "var") {diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsxindex 5ead002d80..9e7ae0c1eb 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -932,7 +932,8 @@ export const CssValueInput = ({{toValue(item.fallback)}</Text>)}- {item.fallback?.type === "rgb" && (+ {(item.fallback?.type === "rgb" ||+ item.fallback?.type === "color") && (<ColorThumb color={toValue(item.fallback)} />)}</Flex>diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.tsindex b028d05fd4..3a918b95bf 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts@@ -454,11 +454,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 0.5,- r: 10,- g: 20,- b: 30,+ colorSpace: "srgb",+ components: [0.0392, 0.0784, 0.1176],});});@@ -469,11 +468,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 1,- r: 255,- g: 0,- b: 0,+ colorSpace: "srgb",+ components: [1, 0, 0],});});@@ -484,11 +482,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 1,- r: 240,- g: 238,- b: 15,+ colorSpace: "srgb",+ components: [0.9412, 0.9333, 0.0588],});});@@ -499,11 +496,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 1,- r: 255,- g: 0,- b: 0,+ colorSpace: "srgb",+ components: [1, 0, 0],});});@@ -514,11 +510,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 1,- r: 240,- g: 238,- b: 15,+ colorSpace: "srgb",+ components: [0.9412, 0.9333, 0.0588],});});@@ -530,11 +525,10 @@ describe("Colors", () => {});expect(result).toEqual({- type: "rgb",+ type: "color",alpha: 0.5,- r: 10,- g: 20,- b: 30,+ colorSpace: "srgb",+ components: [0.0392, 0.0784, 0.1176],});});});@@ -600,10 +594,9 @@ test("parse color in css variable", () => {value: "#0f0f0f",})).toEqual({- type: "rgb",- r: 15,- g: 15,- b: 15,+ type: "color",+ colorSpace: "srgb",+ components: [0.0588, 0.0588, 0.0588],alpha: 1,});});diff --git a/apps/builder/app/builder/features/style-panel/shared/model.tsx b/apps/builder/app/builder/features/style-panel/shared/model.tsxindex dbe064cd45..68e2605ccd 100644--- a/apps/builder/app/builder/features/style-panel/shared/model.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/model.tsx@@ -320,13 +320,24 @@ export const $availableVariables = computed(export const $availableUnitVariables = computed($availableVariables,(availableVariables) =>- availableVariables.filter((value) => value.fallback?.type !== "rgb")+ availableVariables.filter(+ (value) =>+ value.fallback?.type === "unit" ||+ value.fallback?.type === "keyword" ||+ value.fallback?.type === "unparsed"+ ));export const $availableColorVariables = computed($availableVariables,(availableVariables) =>- availableVariables.filter((value) => value.fallback?.type !== "unit")+ availableVariables.filter(+ (value) =>+ value.fallback?.type === "rgb" ||+ value.fallback?.type === "color" ||+ value.fallback?.type === "keyword" ||+ value.fallback?.type === "unparsed"+ ));export const createComputedStyleDeclStore = (property: CssProperty) => {diff --git a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsxindex c01f2d57e1..9cfbcac5f2 100644--- a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx@@ -341,12 +341,20 @@ export const ShadowContent = ({...$availableColorVariables.get(),]}onChange={(value) => {- if (value.type === "rgb" || value.type === "var") {+ if (+ value.type === "rgb" ||+ value.type === "color" ||+ value.type === "var"+ ) {updateShadow({ color: value }, { isEphemeral: true });}}}onChangeComplete={(value) => {- if (value.type === "rgb" || value.type === "var") {+ if (+ value.type === "rgb" ||+ value.type === "color" ||+ value.type === "var"+ ) {updateShadow({ color: value });}}}diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsxindex 271a945741..bbc39043c7 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx@@ -2567,7 +2567,7 @@ describe("Styles", () => {line-height: 44px}Heading {- color: rgb(219 24 24 / 1)+ color: hsl(0 80% 47.78% / 1)}}"`);@@ -2787,7 +2787,7 @@ describe("Styles", () => {expect(toCss(fragment)).toMatchInlineSnapshot(`"@media all {d1 {- color: rgb(209 36 36 / 1)+ color: hsl(0 70.45% 48.11% / 1)}d2 {font-size: 200px@@ -2918,24 +2918,24 @@ describe("Styles", () => {transform: translate3d(7px, 74px, 16px)}x:hover {- background-color: rgb(199 16 16 / 1)+ background-color: hsl(0 85.19% 42.12% / 1)}}@media all and (max-width: 479px) {x:active {- background-color: rgb(189 75 75 / 1)+ background-color: hsl(0 46.06% 51.8% / 1)}x:focus {- background-color: rgb(83 26 170 / 1)+ background-color: hsl(264 73.75% 38.37% / 1)}x:focus-visible {- background-color: rgb(98 67 145 / 1)+ background-color: hsl(264 36.36% 41.56% / 1)}x:hover {- background-color: rgb(214 87 87 / 1)+ background-color: hsl(0 60.99% 58.99% / 1)}x:visited {- background-color: rgb(197 18 18 / 1)+ background-color: hsl(0 83.67% 42.1% / 1)}}"`);@@ -3001,27 +3001,27 @@ describe("Styles", () => {expect(toCss(fragment)).toMatchInlineSnapshot(`"@media all {Div Block 2 {- background-color: rgb(0 208 255 / 1)+ background-color: hsl(191 100% 50% / 1)}}@media all and (max-width: 991px) {Div Block 2 {- background-color: rgb(68 0 255 / 1)+ background-color: hsl(256 100% 50% / 1)}}@media all and (max-width: 767px) {Div Block 2 {- background-color: rgb(255 0 221 / 1)+ background-color: hsl(308 100% 50% / 1)}}@media all and (max-width: 479px) {Div Block 2 {- background-color: rgb(255 0 4 / 1)+ background-color: hsl(359 100% 50% / 1)}}@media all and (min-width: 1280px) {Div Block 2 {- background-color: rgb(0 255 128 / 1)+ background-color: hsl(150 100% 50% / 1)}}"`);diff --git a/apps/builder/app/shared/style-object-model.test.tsx b/apps/builder/app/shared/style-object-model.test.tsxindex 52659d9741..3702d14075 100644--- a/apps/builder/app/shared/style-object-model.test.tsx+++ b/apps/builder/app/shared/style-object-model.test.tsx@@ -438,7 +438,12 @@ test("compute custom properties in shadows", () => {offsetX: { type: "unit", unit: "px", value: 10 },offsetY: { type: "unit", unit: "px", value: 20 },blur: { type: "unit", value: 30, unit: "px" },- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },},],});diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.tsindex da1f36f3f6..a91be0d7f4 100644--- a/packages/css-data/src/parse-css-value.test.ts+++ b/packages/css-data/src/parse-css-value.test.ts@@ -98,31 +98,28 @@ describe("Parse CSS value", () => {describe("Colors", () => {test("Color rgba values", () => {expect(parseCssValue("background-color", "rgba(0,0,0,0)")).toEqual({- type: "rgb",+ type: "color",+ colorSpace: "srgb",alpha: 0,- b: 0,- g: 0,- r: 0,+ components: [0, 0, 0],});});test("modern format", () => {expect(parseCssValue("background-color", "rgb(99 102 241/0.5)")).toEqual({- type: "rgb",- r: 99,- g: 102,- b: 241,+ type: "color",+ colorSpace: "srgb",alpha: 0.5,+ components: [0.3882, 0.4, 0.9451],});});test("Color rgba values", () => {expect(parseCssValue("background-color", "#00220011")).toEqual({- type: "rgb",- alpha: 0.07,- b: 0,- g: 34,- r: 0,+ type: "color",+ colorSpace: "srgb",+ alpha: 0.0667,+ components: [0, 0.1333, 0],});});@@ -132,6 +129,37 @@ describe("Parse CSS value", () => {value: "red",});});++ test("oklch color", () => {+ expect(+ parseCssValue("background-color", "oklch(59.686% 0.1009 29.234)")+ ).toEqual({+ type: "color",+ colorSpace: "oklch",+ alpha: 1,+ components: [0.5969, 0.1009, 29.234],+ });+ });++ test("oklch color with alpha", () => {+ expect(+ parseCssValue("background-color", "oklch(59.686% 0.1009 29.234 / 0.5)")+ ).toEqual({+ type: "color",+ colorSpace: "oklch",+ alpha: 0.5,+ components: [0.5969, 0.1009, 29.234],+ });+ });++ test("oklch color with modern format", () => {+ expect(parseCssValue("color", "oklch(80% 0.15 240)")).toEqual({+ type: "color",+ colorSpace: "oklch",+ alpha: 1,+ components: [0.8, 0.15, 240],+ });+ });});});@@ -590,25 +618,22 @@ test("support unit in custom property", () => {test("support color in custom property", () => {expect(parseCssValue("--color", "rgb(61 77 4)")).toEqual({- type: "rgb",- r: 61,- g: 77,- b: 4,+ type: "color",+ colorSpace: "srgb",alpha: 1,+ components: [0.2392, 0.302, 0.0157],});expect(parseCssValue("--color", "rgba(61, 77, 4, 0.5)")).toEqual({- type: "rgb",- r: 61,- g: 77,- b: 4,+ type: "color",+ colorSpace: "srgb",alpha: 0.5,+ components: [0.2392, 0.302, 0.0157],});expect(parseCssValue("--color", "#3d4d04")).toEqual({- type: "rgb",- r: 61,- g: 77,- b: 4,+ type: "color",+ colorSpace: "srgb",alpha: 1,+ components: [0.2392, 0.302, 0.0157],});expect(parseCssValue("--color", "red")).toEqual({type: "unparsed",@@ -764,7 +789,12 @@ describe("parse shadows", () => {offsetX: { type: "unit", unit: "number", value: 0 },offsetY: { type: "unit", unit: "px", value: 60 },blur: { type: "unit", unit: "px", value: 80 },- color: { alpha: 0.6, b: 0, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0.6,+ components: [0, 0, 0],+ },},{type: "shadow",@@ -772,7 +802,12 @@ describe("parse shadows", () => {offsetX: { type: "unit", unit: "number", value: 0 },offsetY: { type: "unit", unit: "px", value: 45 },blur: { type: "unit", unit: "px", value: 26 },- color: { alpha: 0.14, b: 0, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0.14,+ components: [0, 0, 0],+ },},],});@@ -797,7 +832,12 @@ describe("parse shadows", () => {offsetX: { type: "unit", unit: "number", value: 0 },offsetY: { type: "unit", unit: "number", value: 0 },blur: { type: "unit", unit: "px", value: 5 },- color: { alpha: 0.2, b: 0, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0.2,+ components: [0, 0, 0],+ },},{type: "shadow",@@ -805,7 +845,12 @@ describe("parse shadows", () => {offsetX: { type: "unit", unit: "number", value: 0 },offsetY: { type: "unit", unit: "number", value: 0 },blur: { type: "unit", unit: "px", value: 10 },- color: { alpha: 0.3, b: 0, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0.3,+ components: [0, 0, 0],+ },},{type: "shadow",@@ -813,7 +858,12 @@ describe("parse shadows", () => {offsetX: { type: "unit", unit: "number", value: 0 },offsetY: { type: "unit", unit: "number", value: 0 },blur: { type: "unit", unit: "px", value: 15 },- color: { alpha: 0.4, b: 0, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0.4,+ components: [0, 0, 0],+ },},],});@@ -865,11 +915,17 @@ describe("parse filters", () => {offsetX: { type: "unit", unit: "px", value: 10 },offsetY: { type: "unit", unit: "px", value: 10 },blur: { type: "unit", unit: "px", value: 25 },- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [0, 0, 1],+ },},},],});+expect(parseCssValue("filter", "drop-shadow(10px 10px 25px #0000FF)")).toEqual({@@ -884,7 +940,12 @@ describe("parse filters", () => {offsetX: { type: "unit", unit: "px", value: 10 },offsetY: { type: "unit", unit: "px", value: 10 },blur: { type: "unit", unit: "px", value: 25 },- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [0, 0, 1],+ },},},],diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.tsindex 01f94a0b60..91de556f0d 100644--- a/packages/css-data/src/parse-css-value.ts+++ b/packages/css-data/src/parse-css-value.ts@@ -17,6 +17,7 @@ import {type UnitValue,type LayerValueItem,type RgbValue,+ type ColorValue,type StyleValue,type Unit,type VarValue,@@ -60,7 +61,7 @@ export const isValidDeclaration = (return true;}- // these properties have poor support natively and in csstree+ // these properties have poor support in browser// though rendered styles are merged as shorthand// so validate artificallyif (@@ -71,10 +72,6 @@ export const isValidDeclaration = (return keywordValues[property].includes(value);}- if (property === "transition-behavior") {- return true;- }-// @todo remove after csstree fixes// - https://github.com/csstree/csstree/issues/246// - https://github.com/csstree/csstree/issues/164@@ -92,14 +89,6 @@ export const isValidDeclaration = (if (ast == null) {return false;}- // scale css-proeprty accepts both number and percentage.- // The syntax from MDN is incorrect and should be updated.- // Here is a PR that fixes the same, but it is not merged yet.- // https://github.com/mdn/data/pull/746- if (property === "scale") {- const syntax = "none | [ <number> | <percentage> ]{1,3}";- return lexer.match(syntax, ast).matched !== null;- }if (property === "transition-timing-function" ||@@ -154,21 +143,44 @@ const tupleProps = new Set<CssProperty>([const availableUnits = new Set<string>(Object.values(units).flat());-const parseColor = (colorString: string): undefined | RgbValue => {+// Map color space names to supported ColorValue color spaces+const colorSpace: Record<string, ColorValue["colorSpace"]> = {+ srgb: "srgb",+ "srgb-linear": "srgb-linear",+ "display-p3": "p3",+ p3: "p3",+ hsl: "hsl",+ hwb: "hwb",+ lab: "lab",+ lch: "lch",+ oklab: "oklab",+ oklch: "oklch",+ "a98-rgb": "a98rgb",+ a98rgb: "a98rgb",+ "prophoto-rgb": "prophoto",+ prophoto: "prophoto",+ rec2020: "rec2020",+ "xyz-d65": "xyz-d65",+ "xyz-d50": "xyz-d50",+ xyz: "xyz-d65", // default to d65+};++const toColorComponent = (value: number) =>+ Math.round(value.valueOf() * 10000) / 10000;++export const parseColor = (colorString: string): undefined | ColorValue => {try {- const color = new Color(colorString).to("srgb");- // colorjs.io represents RGB values as 0-1 range- const [r, g, b] = color.coords.map((coord) => coord.valueOf());- const alpha = Number(color.alpha.valueOf().toFixed(2));+ const color = new Color(colorString);return {- type: "rgb",- alpha: alpha ?? 1,- r: Math.round(r * 255),- g: Math.round(g * 255),- b: Math.round(b * 255),+ type: "color",+ colorSpace: colorSpace[color.spaceId],+ components: color.coords.map(+ toColorComponent+ ) as ColorValue["components"],+ alpha: toColorComponent(color.alpha),};} catch {- return;+ // Invalid colors or relative color syntax are treated as unparsed}};@@ -178,7 +190,7 @@ const parseShadow = (): ShadowValue | UnparsedValue => {// https://drafts.csswg.org/css-borders-4/#box-shadow-positionlet position: "inset" | "outset" = "outset";- let color: undefined | RgbValue | KeywordValue;+ let color: undefined | ColorValue | RgbValue | KeywordValue;const units: UnitValue[] = [];for (const node of nodes) {const item = parseLiteral(node, ["inset"]);@@ -186,6 +198,8 @@ const parseShadow = (position = item.value;} else if (item?.type === "keyword" && parseColor(item.value)) {color = item;+ } else if (item?.type === "color") {+ color = item;} else if (item?.type === "rgb") {color = item;} else if (item?.type === "unit") {@@ -243,6 +257,7 @@ const parseLiteral = (| KeywordValue| ImageValue| RgbValue+ | ColorValue| VarValue| FunctionValue => {if (node?.type === "Number") {@@ -299,7 +314,13 @@ const parseLiteral = (node.name === "hsl" ||node.name === "hsla" ||node.name === "rgb" ||- node.name === "rgba"+ node.name === "rgba" ||+ node.name === "oklch" ||+ node.name === "oklab" ||+ node.name === "lch" ||+ node.name === "lab" ||+ node.name === "hwb" ||+ node.name === "color") {const color = parseColor(generate(node));if (color) {@@ -444,7 +465,8 @@ export const parseCssValue = (if (parsedValue?.type === "var" ||parsedValue?.type === "unit" ||- parsedValue?.type === "rgb"+ parsedValue?.type === "rgb" ||+ parsedValue?.type === "color") {return parsedValue;}@@ -487,17 +509,6 @@ export const parseCssValue = (return layersValue;}- // csstree does not support transition-behavior- // so check keywords manually- if (property === "transition-behavior") {- const node = ast.type === "Value" ? ast.children.first : ast;- const keyword = parseLiteral(node, keywordValues[property]);- if (keyword?.type === "keyword") {- return keyword;- }- return invalidValue;- }-if (property === "font-family") {return {type: "fontFamily",diff --git a/packages/css-data/src/parse-css.test.ts b/packages/css-data/src/parse-css.test.tsindex 50b1b00d04..3b82ed04f5 100644--- a/packages/css-data/src/parse-css.test.ts+++ b/packages/css-data/src/parse-css.test.ts@@ -17,7 +17,12 @@ describe("Parse CSS", () => {{selector: ".test",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -147,7 +152,12 @@ describe("Parse CSS", () => {{selector: ".test",property: "background-color",- value: { alpha: 1, b: 252, g: 255, r: 235, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [0.9216, 1, 0.9882],+ },},]);});@@ -200,7 +210,12 @@ describe("Parse CSS", () => {selector: "a",state: ":hover",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -210,7 +225,12 @@ describe("Parse CSS", () => {{selector: '[class^="a"]',property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -222,7 +242,12 @@ describe("Parse CSS", () => {selector: ":first-pseudo",state: ":my-state",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -233,7 +258,12 @@ describe("Parse CSS", () => {selector: "input",state: "::placeholder",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -243,13 +273,23 @@ describe("Parse CSS", () => {{selector: "a",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},{selector: "a",state: ":hover",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -260,13 +300,23 @@ describe("Parse CSS", () => {selector: "a",state: ":active",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},{selector: "a",state: ":hover",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -286,11 +336,10 @@ describe("Parse CSS", () => {state: ":hover",property: "color",value: {+ type: "color",+ colorSpace: "srgb",alpha: 1,- b: 0,- g: 0,- r: 255,- type: "rgb",+ components: [1, 0, 0],},},]);@@ -495,7 +544,12 @@ describe("Parse CSS", () => {{selector: "a > b",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -505,7 +559,12 @@ describe("Parse CSS", () => {{selector: ".a b",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -515,7 +574,12 @@ describe("Parse CSS", () => {{selector: "a b c.d",property: "color",- value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 1,+ components: [1, 0, 0],+ },},]);});@@ -567,7 +631,12 @@ test("parse incorrectly unprefixed tap-highlight-color", () => {{selector: "a",property: "-webkit-tap-highlight-color",- value: { alpha: 0, b: 0, g: 0, r: 0, type: "rgb" },+ value: {+ type: "color",+ colorSpace: "srgb",+ alpha: 0,+ components: [0, 0, 0],+ },},{selector: "b",diff --git a/packages/css-data/src/property-parsers/conic-gradient.test.ts b/packages/css-data/src/property-parsers/conic-gradient.test.tsindex 127e8289ab..5827ab351c 100644--- a/packages/css-data/src/property-parsers/conic-gradient.test.ts+++ b/packages/css-data/src/property-parsers/conic-gradient.test.ts@@ -17,17 +17,32 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 50 },hint: undefined,},{- color: { type: "rgb", r: 255, g: 255, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -45,12 +60,22 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 165, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.6471, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 255, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 1, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -68,12 +93,22 @@ describe("parse conic-gradient", () => {position: "center",stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -93,22 +128,42 @@ describe("parse conic-gradient", () => {position: "50% 50%",stops: [{- color: { type: "rgb", r: 255, g: 126, b: 95, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.4941, 0.3725],+ alpha: 1,+ },position: { type: "unit", unit: "deg", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 254, g: 180, b: 123, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0.9961, 0.7059, 0.4824],+ alpha: 1,+ },position: { type: "unit", unit: "deg", value: 120 },hint: undefined,},{- color: { type: "rgb", r: 134, g: 168, b: 231, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0.5255, 0.6588, 0.9059],+ alpha: 1,+ },position: { type: "unit", unit: "deg", value: 240 },hint: undefined,},{- color: { type: "rgb", r: 255, g: 126, b: 95, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.4941, 0.3725],+ alpha: 1,+ },position: { type: "unit", unit: "deg", value: 360 },hint: undefined,},@@ -128,12 +183,22 @@ describe("parse conic-gradient", () => {repeating: true,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: { type: "unit", unit: "%", value: 25 },},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 50 },hint: { type: "unit", unit: "%", value: 75 },},@@ -198,12 +263,22 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -222,7 +297,12 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: {type: "var",value: "start",@@ -235,7 +315,12 @@ describe("parse conic-gradient", () => {},},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "var", value: "end" },hint: undefined,},@@ -257,17 +342,32 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 0, g: 128, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0.502, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 50 },hint: undefined,},{- color: { type: "rgb", r: 255, g: 255, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -289,17 +389,32 @@ describe("parse conic-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 255, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 1, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},diff --git a/packages/css-data/src/property-parsers/gradient-utils.ts b/packages/css-data/src/property-parsers/gradient-utils.tsindex a718420686..3285d4394e 100644--- a/packages/css-data/src/property-parsers/gradient-utils.ts+++ b/packages/css-data/src/property-parsers/gradient-utils.ts@@ -1,12 +1,11 @@import * as csstree from "css-tree";-import { parseCssValue } from "../parse-css-value";+import { parseColor, parseCssValue } from "../parse-css-value";import {type UnitValue,type Unit,type VarValue,toValue,} from "@webstudio-is/css-engine";-import Color from "colorjs.io";import type { GradientColorValue, GradientStop } from "./types";export const angleUnitIdentifiers = ["deg", "grad", "rad", "turn"] as const;@@ -105,32 +104,19 @@ export const getColor = () {return;}-const css = csstree.generate(node).trim();if (css.length === 0) {return;}-const parsed = parseCssValue("color", css);- if (parsed.type === "rgb" || parsed.type === "var") {+ if (+ parsed.type === "color" ||+ parsed.type === "rgb" ||+ parsed.type === "var"+ ) {return parsed;}- if (parsed.type === "keyword") {- try {- const color = new Color(parsed.value);- const [r, g, b] = color.coords;- const alpha = color.alpha;- return {- type: "rgb",- r: r * 255,- g: g * 255,- b: b * 255,- alpha: alpha ?? 1,- } satisfies GradientColorValue;- } catch {- // Invalid color- }- }+ return parseColor(css);};export const isColorStop = (node: csstree.CssNode): boolean =>diff --git a/packages/css-data/src/property-parsers/linear-gradient.test.ts b/packages/css-data/src/property-parsers/linear-gradient.test.tsindex a7e6551f85..f4aa1429dd 100644--- a/packages/css-data/src/property-parsers/linear-gradient.test.ts+++ b/packages/css-data/src/property-parsers/linear-gradient.test.ts@@ -15,17 +15,32 @@ describe("parses linear-gradient", () => {sideOrCorner: undefined,stops: [{- color: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 0, g: 255, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},@@ -43,13 +58,23 @@ describe("parses linear-gradient", () => {sideOrCorner: undefined,stops: [{- color: { alpha: 1, b: 0, g: 165, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.6471, 0],+ alpha: 1,+ },hint: { type: "unit", unit: "%", value: 20 },position: { type: "unit", unit: "%", value: 60 },},{ hint: { type: "unit", unit: "%", value: 40 } },{- color: { alpha: 1, b: 255, g: 255, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 1, 1],+ alpha: 1,+ },hint: undefined,position: undefined,},@@ -69,22 +94,42 @@ describe("parses linear-gradient", () => {sideOrCorner: { type: "keyword", value: "to top right" },stops: [{- color: { alpha: 1, b: 0, g: 165, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.6471, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 0, g: 255, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 0, g: 128, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0.502, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},@@ -104,27 +149,52 @@ describe("parses linear-gradient", () => {sideOrCorner: { type: "keyword", value: "to right" },stops: [{- color: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },hint: undefined,position: { type: "unit", unit: "%", value: 20 },},{- color: { alpha: 1, b: 0, g: 165, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.6471, 0],+ alpha: 1,+ },hint: { type: "unit", unit: "%", value: 40 },position: { type: "unit", unit: "%", value: 20 },},{- color: { alpha: 1, b: 0, g: 255, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },hint: { type: "unit", unit: "%", value: 60 },position: { type: "unit", unit: "%", value: 40 },},{- color: { alpha: 1, b: 0, g: 128, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0.502, 0],+ alpha: 1,+ },hint: { type: "unit", unit: "%", value: 80 },position: { type: "unit", unit: "%", value: 60 },},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },hint: undefined,position: { type: "unit", unit: "%", value: 80 },},@@ -175,7 +245,12 @@ describe("parses linear-gradient", () => {position: undefined,},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },hint: undefined,position: undefined,},@@ -204,7 +279,12 @@ describe("parses linear-gradient", () => {hint: { type: "unit", unit: "%", value: 50 },},{- color: { alpha: 1, b: 0, g: 255, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},@@ -223,7 +303,12 @@ describe("parses linear-gradient", () => {sideOrCorner: { type: "keyword", value: "to bottom" },stops: [{- color: { alpha: 1, b: 0, g: 128, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0.502, 0],+ alpha: 1,+ },hint: undefined,position: { type: "unit", unit: "%", value: 0 },},@@ -252,7 +337,12 @@ describe("parses linear-gradient", () => {sideOrCorner: undefined,stops: [{- color: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },hint: undefined,position: {type: "var",@@ -261,7 +351,12 @@ describe("parses linear-gradient", () => {},},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },hint: undefined,position: { type: "var", value: "end" },},@@ -286,7 +381,12 @@ describe("parses linear-gradient", () => {sideOrCorner: undefined,stops: [{- color: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: {type: "var",@@ -295,7 +395,12 @@ describe("parses linear-gradient", () => {},},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -372,12 +477,207 @@ describe("parses linear-gradient", () => {sideOrCorner: undefined,stops: [{- color: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ {+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ ],+ })+ );+ });++ test("parses linear-gradient with oklch colors", () => {+ expect(+ parseLinearGradient(+ "linear-gradient(oklch(0.6 0.1 180), oklch(0.8 0.15 240))"+ )+ ).toEqual(+ linear({+ angle: undefined,+ sideOrCorner: undefined,+ stops: [+ {+ color: {+ type: "color",+ colorSpace: "oklch",+ components: [0.6, 0.1, 180],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ {+ color: {+ type: "color",+ colorSpace: "oklch",+ components: [0.8, 0.15, 240],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ ],+ })+ );+ });++ test("parses linear-gradient with hsl colors", () => {+ expect(+ parseLinearGradient(+ "linear-gradient(hsl(180 100% 50%), hsl(240 100% 50%))"+ )+ ).toEqual(+ linear({+ angle: undefined,+ sideOrCorner: undefined,+ stops: [+ {+ color: {+ type: "color",+ colorSpace: "hsl",+ components: [180, 100, 50],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ {+ color: {+ type: "color",+ colorSpace: "hsl",+ components: [240, 100, 50],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ ],+ })+ );+ });++ test("parses linear-gradient with color() function", () => {+ expect(+ parseLinearGradient(+ "linear-gradient(color(srgb 1 0 0), color(srgb 0 0 1))"+ )+ ).toEqual(+ linear({+ angle: undefined,+ sideOrCorner: undefined,+ stops: [+ {+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },hint: undefined,position: undefined,},{- color: { alpha: 1, b: 255, g: 0, r: 0, type: "rgb" },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },+ hint: undefined,+ position: undefined,+ },+ ],+ })+ );+ });++ test("parses linear-gradient with mixed modern color spaces", () => {+ expect(+ parseLinearGradient(+ "linear-gradient(90deg, oklch(0.7 0.2 30) 0%, hsl(120deg 50% 60%) 50%, color(display-p3 0 1 0) 100%)"+ )+ ).toEqual(+ linear({+ angle: { type: "unit", unit: "deg", value: 90 },+ sideOrCorner: undefined,+ stops: [+ {+ color: {+ type: "color",+ colorSpace: "oklch",+ components: [0.7, 0.2, 30],+ alpha: 1,+ },+ hint: undefined,+ position: { type: "unit", unit: "%", value: 0 },+ },+ {+ color: {+ type: "color",+ colorSpace: "hsl",+ components: [120, 50, 60],+ alpha: 1,+ },+ hint: undefined,+ position: { type: "unit", unit: "%", value: 50 },+ },+ {+ color: {+ type: "color",+ colorSpace: "p3",+ components: [0, 1, 0],+ alpha: 1,+ },+ hint: undefined,+ position: { type: "unit", unit: "%", value: 100 },+ },+ ],+ })+ );+ });++ test("parses linear-gradient with oklch colors with alpha", () => {+ expect(+ parseLinearGradient(+ "linear-gradient(oklch(0.6 0.1 180 / 0.5), oklch(0.8 0.15 240 / 0.8))"+ )+ ).toEqual(+ linear({+ angle: undefined,+ sideOrCorner: undefined,+ stops: [+ {+ color: {+ type: "color",+ colorSpace: "oklch",+ components: [0.6, 0.1, 180],+ alpha: 0.5,+ },+ hint: undefined,+ position: undefined,+ },+ {+ color: {+ type: "color",+ colorSpace: "oklch",+ components: [0.8, 0.15, 240],+ alpha: 0.8,+ },hint: undefined,position: undefined,},diff --git a/packages/css-data/src/property-parsers/radial-gradient.test.ts b/packages/css-data/src/property-parsers/radial-gradient.test.tsindex 07198dd488..28c3bdd464 100644--- a/packages/css-data/src/property-parsers/radial-gradient.test.ts+++ b/packages/css-data/src/property-parsers/radial-gradient.test.ts@@ -17,7 +17,12 @@ describe("parse radial-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -36,17 +41,32 @@ describe("parse radial-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 255, g: 255, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 1, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -67,12 +87,22 @@ describe("parse radial-gradient", () => {position: "center",stops: [{- color: { type: "rgb", r: 255, g: 165, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0.6471, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 255, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 1, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -93,12 +123,22 @@ describe("parse radial-gradient", () => {position: "left top",stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 0 },hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 100 },hint: undefined,},@@ -117,12 +157,22 @@ describe("parse radial-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -141,12 +191,22 @@ describe("parse radial-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -165,12 +225,22 @@ describe("parse radial-gradient", () => {position: undefined,stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -191,12 +261,22 @@ describe("parse radial-gradient", () => {position: "40px 40px",stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: undefined,hint: undefined,},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: undefined,hint: undefined,},@@ -217,12 +297,22 @@ describe("parse radial-gradient", () => {position: "10px 20px",stops: [{- color: { type: "rgb", r: 255, g: 0, b: 0, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [1, 0, 0],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 10 },hint: { type: "unit", unit: "%", value: 40 },},{- color: { type: "rgb", r: 0, g: 0, b: 255, alpha: 1 },+ color: {+ type: "color",+ colorSpace: "srgb",+ components: [0, 0, 1],+ alpha: 1,+ },position: { type: "unit", unit: "%", value: 80 },hint: undefined,},diff --git a/packages/css-data/src/property-parsers/types.ts b/packages/css-data/src/property-parsers/types.tsindex 2adb1858ba..2b30d06967 100644--- a/packages/css-data/src/property-parsers/types.ts+++ b/packages/css-data/src/property-parsers/types.ts@@ -1,11 +1,16 @@import {+ type ColorValue,type KeywordValue,type RgbValue,type UnitValue,type VarValue,} from "@webstudio-is/css-engine";-export type GradientColorValue = RgbValue | KeywordValue | VarValue;+export type GradientColorValue =+ | ColorValue+ | RgbValue+ | KeywordValue+ | VarValue;export type GradientStop = {color?: GradientColorValue;diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.tsindex efe8b096d6..3ad7e65f95 100644--- a/packages/css-data/src/shorthands.ts+++ b/packages/css-data/src/shorthands.ts@@ -544,8 +544,7 @@ const expandTransition = function* (value: CssNode) {"<time>","<easing-function>","<time>",- // <transition-behavior-value> is not supported by csstree- "normal | allow-discrete",+ "<transition-behavior-value>",],single);@@ -864,12 +863,7 @@ const expandWhiteSpace = function* (value: CssNode) {let collapse = collapseKeyword;let wrapMode = wrapKeyword;[collapse = collapseKeyword, wrapMode = wrapKeyword] = parseUnordered(- [- // <'white-space-collapse'> is not supported by csstree- "collapse | preserve | preserve-breaks | preserve-spaces | break-spaces",- // <'text-wrap-mode'> is not supported by csstree- "wrap | nowrap",- ],+ ["<'white-space-collapse'>", "<'text-wrap-mode'>"],value);if (lexer.match("normal", value).matched) {@@ -1369,15 +1363,7 @@ const expandShorthand = function* (property: string, value: CssNode) {const [mode = createIdentifier("wrap"),style = createIdentifier("auto"),- ] = parseUnordered(- [- // <'text-wrap-mode'> is not supported by csstree- "wrap | nowrap",- // <'text-wrap-style'> is not supported by csstree- "auto | balance | stable | pretty",- ],- value- );+ ] = parseUnordered(["<'text-wrap-mode'>", "<'text-wrap-style'>"], value);yield ["text-wrap-mode", mode] as const;yield ["text-wrap-style", style] as const;break;diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.tsindex adf3fcd705..ea69d3ccfb 100644--- a/packages/css-engine/src/core/to-value.test.ts+++ b/packages/css-engine/src/core/to-value.test.ts@@ -241,7 +241,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {components: [1, 0.5, 0.2],alpha: 1,});- expect(value).toBe("rgb(255 127.5 51 / 1)");+ expect(value).toBe("rgb(255 128 51 / 1)");});test("color with srgb and alpha channel", () => {@@ -261,7 +261,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {components: [0.5, 0.5, 0.5],alpha: 0,});- expect(value).toBe("rgb(127.5 127.5 127.5 / 0)");+ expect(value).toBe("rgb(128 128 128 / 0)");});test("color with hsl color space", () => {diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.tsindex 140c50f4ec..d08d1f70f8 100644--- a/packages/css-engine/src/core/to-value.ts+++ b/packages/css-engine/src/core/to-value.ts@@ -88,13 +88,17 @@ export const toValue = (}if (value.type === "color") {- const [c1, c2, c3] = value.components;+ let [c1, c2, c3] = value.components;const alpha = value.alpha;// Use specific CSS functions when availableswitch (value.colorSpace) {- case "srgb":- return `rgb(${c1 * 255} ${c2 * 255} ${c3 * 255} / ${alpha})`;+ case "srgb": {+ c1 = Math.round(c1 * 255);+ c2 = Math.round(c2 * 255);+ c3 = Math.round(c3 * 255);+ return `rgb(${c1} ${c2} ${c3} / ${alpha})`;+ }case "hsl":return `hsl(${c1} ${c2}% ${c3}% / ${alpha})`;case "hwb":diff --git a/packages/css-engine/src/schema.ts b/packages/css-engine/src/schema.tsindex 1edab23349..fbca75503a 100644--- a/packages/css-engine/src/schema.ts+++ b/packages/css-engine/src/schema.ts@@ -66,6 +66,7 @@ export type RgbValue = z.infer<typeof RgbValue>;export const ColorValue = z.object({type: z.literal("color"),+ // all these color spaces are defined by design tokens specificationcolorSpace: z.union([z.literal("srgb"),z.literal("p3"),diff --git a/packages/design-system/package.json b/packages/design-system/package.jsonindex f600931ea7..83c5455382 100644--- a/packages/design-system/package.json+++ b/packages/design-system/package.json@@ -61,9 +61,9 @@"@webstudio-is/css-data": "workspace:*","@webstudio-is/css-engine": "workspace:*","@webstudio-is/icons": "workspace:*",- "colorjs.io": "^0.5.0","change-case": "^5.4.4","cmdk": "^1.1.1",+ "colorjs.io": "^0.5.2","downshift": "^6.1.7","match-sorter": "^8.0.0","react-colorful": "^5.6.1",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex ea50a7239a..b517d392b7 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1427,7 +1427,7 @@ importers:specifier: ^1.1.1version: 1.1.1(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])colorjs.io:- specifier: ^0.5.0+ specifier: ^0.5.2version: 0.5.2downshift:specifier: ^6.1.7