Skip to content

AI-native design editor. Open-source Figma alternative.

Use colorToCSS() instead of hand-rolled rgba conversions

Danila Poyarkovb4698d7a639d
diff --git a/packages/core/src/color.ts b/packages/core/src/color.ts
index 2311b33f6..2ec2cac13 100644
--- a/packages/core/src/color.ts
+++ b/packages/core/src/color.ts
@@ -36,8 +36,9 @@ export function colorToRgba255(color: Color) {
}
export function colorToCSS(color: Color): string {
- const { r, g, b } = colorToRgba255(color)
- return `rgb(${r}, ${g}, ${b})`
+ const { r, g, b, a } = colorToRgba255(color)
+ if (a >= 1) return `rgb(${r}, ${g}, ${b})`
+ return `rgba(${r}, ${g}, ${b}, ${a})`
}
export function rgba255ToColor(rgba: Color): Color {
diff --git a/src/components/ColorPicker.vue b/src/components/ColorPicker.vue
index 2727637d4..6ff7032f3 100644
--- a/src/components/ColorPicker.vue
+++ b/src/components/ColorPicker.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { colorToCSS } from '@open-pencil/core'
import { computed } from 'vue'
import { PopoverRoot, PopoverTrigger, PopoverPortal, PopoverContent } from 'reka-ui'
@@ -14,10 +15,7 @@ const emit = defineEmits<{
update: [color: Color]
}>()
-const swatchColor = computed(() => {
- const c = props.color
- return `rgba(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)}, ${c.a})`
-})
+const swatchColor = computed(() => colorToCSS(props.color))
</script>
<template>
diff --git a/src/components/FillPicker.vue b/src/components/FillPicker.vue
index 3f594ea38..13d15971a 100644
--- a/src/components/FillPicker.vue
+++ b/src/components/FillPicker.vue
@@ -18,10 +18,10 @@ import {
import HsvColorArea from './HsvColorArea.vue'
import ScrubInput from './ScrubInput.vue'
-import { colorToHexRaw, colorToRgba255, parseColor } from '@/engine/color'
+import { colorToCSS, colorToHexRaw, parseColor } from '@open-pencil/core'
import type { Color } from '@/types'
-import type { Fill, GradientStop, GradientTransform } from '@/engine/scene-graph'
+import type { Fill, GradientStop, GradientTransform } from '@open-pencil/core'
type FillCategory = 'SOLID' | 'GRADIENT' | 'IMAGE'
type GradientSubtype =
@@ -172,29 +172,20 @@ function updateStopOpacity(index: number, value: string) {
emit('update', { ...props.fill, gradientStops: stops })
}
+function gradientStops(stops: GradientStop[]): string {
+ return stops.map((s) => `${colorToCSS(s.color)} ${s.position * 100}%`).join(', ')
+}
+
const swatchBackground = computed(() => {
if (isGradient.value && props.fill.gradientStops?.length) {
- const stops = props.fill.gradientStops
- .map((s) => {
- const rgba = colorToRgba255(s.color)
- return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${s.color.a}) ${s.position * 100}%`
- })
- .join(', ')
- return `linear-gradient(to right, ${stops})`
+ return `linear-gradient(to right, ${gradientStops(props.fill.gradientStops)})`
}
- const rgba = colorToRgba255(props.fill.color)
- return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${props.fill.color.a})`
+ return colorToCSS(props.fill.color)
})
const gradientBarBackground = computed(() => {
if (!props.fill.gradientStops?.length) return ''
- const stops = props.fill.gradientStops
- .map((s) => {
- const rgba = colorToRgba255(s.color)
- return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${s.color.a}) ${s.position * 100}%`
- })
- .join(', ')
- return `linear-gradient(to right, ${stops})`
+ return `linear-gradient(to right, ${gradientStops(props.fill.gradientStops)})`
})
const gradientStopBarRef = ref<HTMLDivElement | null>(null)
@@ -222,8 +213,7 @@ function onStopBarPointerUp() {
}
function stopSwatchColor(stop: GradientStop) {
- const rgba = colorToRgba255(stop.color)
- return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${stop.color.a})`
+ return colorToCSS(stop.color)
}
</script>
diff --git a/src/components/properties/EffectsSection.vue b/src/components/properties/EffectsSection.vue
index ddd9fbc4c..9821f8f4c 100644
--- a/src/components/properties/EffectsSection.vue
+++ b/src/components/properties/EffectsSection.vue
@@ -6,7 +6,9 @@ import ScrubInput from '@/components/ScrubInput.vue'
import { useNodeProps } from '@/composables/use-node-props'
import { useMultiProps } from '@/composables/use-multi-props'
-import type { Color, Effect } from '@/engine/scene-graph'
+import { colorToCSS } from '@open-pencil/core'
+
+import type { Color, Effect } from '@open-pencil/core'
const { store } = useNodeProps()
const { node, nodes, isMulti, active } = useMultiProps()
@@ -168,9 +170,7 @@ function toggleExpand(index: number) {
<button
v-if="isShadow(effect.type)"
class="size-5 shrink-0 cursor-pointer rounded border border-border"
- :style="{
- background: `rgba(${Math.round(effect.color.r * 255)}, ${Math.round(effect.color.g * 255)}, ${Math.round(effect.color.b * 255)}, ${effect.color.a})`
- }"
+ :style="{ background: colorToCSS(effect.color) }"
@click="toggleExpand(i)"
/>
<button
diff --git a/src/components/properties/FillSection.vue b/src/components/properties/FillSection.vue
index 51754403f..6b49e997f 100644
--- a/src/components/properties/FillSection.vue
+++ b/src/components/properties/FillSection.vue
@@ -18,8 +18,8 @@ import ScrubInput from '@/components/ScrubInput.vue'
import { useNodeProps } from '@/composables/use-node-props'
import { useMultiProps } from '@/composables/use-multi-props'
import { DEFAULT_SHAPE_FILL } from '@/constants'
-import { colorToHexRaw } from '@/engine/color'
-import type { Fill, Variable, Color } from '@/engine/scene-graph'
+import { colorToCSS, colorToHexRaw } from '@open-pencil/core'
+import type { Fill, Variable, Color } from '@open-pencil/core'
const { store } = useNodeProps()
const { nodes, isMulti, active, activeNode } = useMultiProps()
@@ -57,7 +57,7 @@ function unbindVariable(index: number) {
function resolvedSwatchStyle(variable: Variable): string {
const color = store.graph.resolveColorVariable(variable.id)
if (!color) return 'background: #000'
- return `background: rgb(${Math.round(color.r * 255)}, ${Math.round(color.g * 255)}, ${Math.round(color.b * 255)})`
+ return `background: ${colorToCSS(color)}`
}
function updateFill(index: number, fill: Fill) {