feat: add public ColorArea components, docs, and package contract

This commit is contained in:
maoyingjie
2026-10-26 23:51:31 +08:00
parent 01882160e6
commit ae364578ce
33 changed files with 2201 additions and 151 deletions
@@ -0,0 +1,187 @@
<script setup lang="ts">
import { computed, ref, shallowRef } from "vue";
import {
ColorArea,
ColorAreaThumb,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Select,
SelectIndicator,
SelectPopover,
SelectTrigger,
SelectValue,
parseColor,
type Color,
type ColorAxisChannel,
type ColorSpace,
type SelectModelValue,
} from "@heroui-vue/components";
interface ChannelOption {
id: ColorAxisChannel;
name: string;
}
const colorSpaces: Array<{ id: ColorSpace; name: string }> = [
{ id: "rgb", name: "RGB" },
{ id: "hsl", name: "HSL" },
{ id: "hsb", name: "HSB" },
];
const channelsBySpace: Record<ColorSpace, ChannelOption[]> = {
hsb: [
{ id: "hue", name: "Hue" },
{ id: "saturation", name: "Saturation" },
{ id: "brightness", name: "Brightness" },
],
hsl: [
{ id: "hue", name: "Hue" },
{ id: "saturation", name: "Saturation" },
{ id: "lightness", name: "Lightness" },
],
rgb: [
{ id: "red", name: "Red" },
{ id: "green", name: "Green" },
{ id: "blue", name: "Blue" },
],
};
const color = shallowRef<Color | null>(parseColor("hsb(219, 58%, 93%)"));
const colorSpace = ref<ColorSpace>("hsb");
const xChannel = ref<ColorAxisChannel>("saturation");
const yChannel = ref<ColorAxisChannel>("brightness");
const channels = computed(() => channelsBySpace[colorSpace.value]);
const xChannelOptions = computed(() =>
channels.value.filter((channel) => channel.id !== yChannel.value),
);
const yChannelOptions = computed(() =>
channels.value.filter((channel) => channel.id !== xChannel.value),
);
function handleColorSpaceChange(value: SelectModelValue) {
if (typeof value !== "string") return;
const space = value as ColorSpace;
colorSpace.value = space;
if (space === "rgb") {
xChannel.value = "blue";
yChannel.value = "green";
} else if (space === "hsl") {
xChannel.value = "saturation";
yChannel.value = "lightness";
} else {
xChannel.value = "saturation";
yChannel.value = "brightness";
}
}
function handleXChannel(value: SelectModelValue) {
if (typeof value === "string") xChannel.value = value as ColorAxisChannel;
}
function handleYChannel(value: SelectModelValue) {
if (typeof value === "string") yChannel.value = value as ColorAxisChannel;
}
</script>
<template>
<div class="flex flex-col items-center gap-6">
<div class="flex gap-4">
<Select
class="w-32"
:model-value="colorSpace"
@update:model-value="handleColorSpaceChange"
>
<Label>Color Space</Label>
<SelectTrigger>
<SelectValue />
<SelectIndicator />
</SelectTrigger>
<SelectPopover>
<ListBox>
<ListBoxItem
v-for="space in colorSpaces"
:key="space.id"
:id="space.id"
:text-value="space.name"
>
{{ space.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</SelectPopover>
</Select>
<Select
class="w-36"
:model-value="xChannel"
@update:model-value="handleXChannel"
>
<Label>X Axis</Label>
<SelectTrigger>
<SelectValue />
<SelectIndicator />
</SelectTrigger>
<SelectPopover>
<ListBox>
<ListBoxItem
v-for="channel in xChannelOptions"
:key="channel.id"
:id="channel.id"
:text-value="channel.name"
>
{{ channel.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</SelectPopover>
</Select>
<Select
class="w-36"
:model-value="yChannel"
@update:model-value="handleYChannel"
>
<Label>Y Axis</Label>
<SelectTrigger>
<SelectValue />
<SelectIndicator />
</SelectTrigger>
<SelectPopover>
<ListBox>
<ListBoxItem
v-for="channel in yChannelOptions"
:key="channel.id"
:id="channel.id"
:text-value="channel.name"
>
{{ channel.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</SelectPopover>
</Select>
</div>
<ColorArea
v-model="color"
:color-space="colorSpace"
:x-channel="xChannel"
:y-channel="yChannel"
>
<ColorAreaThumb />
</ColorArea>
<div class="flex items-center gap-3">
<div
class="size-8 rounded-md border border-default"
:style="{ backgroundColor: color?.toString('css') }"
/>
<code class="rounded bg-default/50 px-2 py-1 text-sm">
{{ color?.toString(colorSpace) ?? "(empty)" }}
</code>
</div>
</div>
</template>