@molecule/app-color-picker-svelte

Feature · color-picker · App (browser) · v1.1.1 · Apache-2.0

HSV slider + RGB sliders + HEX input — controlled color picker for design canvases, photo editors, animation tools

npm install @molecule/app-color-picker-svelte

npm · Source on GitHub

How it works

@molecule/app-color-picker-svelte is a ready-made color-picker feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<script lang="ts">
  import { ColorPicker } from '@molecule/app-color-picker-svelte'

  let color = $state('#3366ff')
</script>

<ColorPicker value={color} onChange={(next) => (color = next)} />

Works with: @molecule/app-i18n, @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte

Reference

Svelte controlled HSV + RGB + HEX color picker — design canvases, photo editors, animation tools, brand editors.

The Svelte counterpart of @molecule/app-color-picker-react: same exports, same props (React names), same DOM for the same input.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<script lang="ts">
  import { ColorPicker } from '@molecule/app-color-picker-svelte'

  let color = $state('#3366ff')
</script>

<ColorPicker value={color} onChange={(next) => (color = next)} />

Type

feature

Installation

npm install @molecule/app-color-picker-svelte @molecule/app-i18n @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte

API

Interfaces

RgbColor

RGB color, 8-bit components (0–255).

interface RgbColor {
  /** Red (0–255). */
  r: number
  /** Green (0–255). */
  g: number
  /** Blue (0–255). */
  b: number
}

HsvColor

HSV color, hue degrees + saturation/value 0–1.

interface HsvColor {
  /** Hue in degrees (0–360). */
  h: number
  /** Saturation (0–1). */
  s: number
  /** Value / brightness (0–1). */
  v: number
}

ColorPickerProps

Props for the ColorPicker component.

interface ColorPickerProps {
  /** Current color as a 6-character HEX string with leading `#`. */
  value: string
  /** Called whenever the user picks a new color. Receives a `#rrggbb` string. */
  onChange: (hex: string) => void
  /** `data-mol-id` attribute for AI-agent selectors. */
  dataMolId?: string
  /** Extra classes appended via the ClassMap `cn()` helper. */
  className?: string
}

Functions

clamp(n, lo, hi)

Clamp a number to the inclusive range [lo, hi].

function clamp(n: number, lo: number, hi: number): number
  • n — Input value.
  • lo — Lower bound.
  • hi — Upper bound.

Returns: The clamped value.

hexToRgb(hex)

Parse a #rgb/#rrggbb hex string into an RGB color. Falls back to black for unparseable input rather than throwing — pickers shouldn't crash on a stray empty string.

function hexToRgb(hex: string): RgbColor
  • hex — Color string (with or without the leading #).

Returns: The parsed RGB color.

rgbToHex(rgb)

Format an RGB color as a #rrggbb hex string (lowercase).

function rgbToHex({ r, g, b }: RgbColor): string
  • rgb — Color to format.

Returns: The hex string.

rgbToHsv(rgb)

Convert RGB (0–255) to HSV (h: 0–360, s/v: 0–1).

function rgbToHsv({ r, g, b }: RgbColor): HsvColor
  • rgb — Color to convert.

Returns: The equivalent HSV color.

hsvToRgb(hsv)

Convert HSV (h: 0–360, s/v: 0–1) to RGB (0–255).

function hsvToRgb({ h, s, v }: HsvColor): RgbColor
  • hsv — Color to convert.

Returns: The equivalent RGB color.

isValidHex(hex)

Whether hex parses to a 6-character hex color.

function isValidHex(hex: string): boolean
  • hex — Candidate hex string (with or without #).

Returns: true when the string parses cleanly.

Constants

ColorPicker

const ColorPicker: LegacyComponentType

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-i18n ^1.0.1
  • @molecule/app-svelte ^3.5.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-svelte ^1.1.0
  • svelte ^5.0.0

Runtime Dependencies

  • @molecule/app-i18n

  • @molecule/app-svelte

  • @molecule/app-ui

  • @molecule/app-ui-svelte

  • svelte

  • Requires a bonded ClassMap (getClassMap()) and the binding's i18n provider for the translated aria-labels — rendering throws without a ClassMap.

  • Controlled: the parent owns value and receives every change through onChange. Pure UI — render it inside whatever popover you prefer.

Translations

Translation strings are provided by @molecule/app-locales-color-picker.