import type { HslColorValue, HsvColorValue, LinearRgbColorValue, SrgbColorValue } from "../types.ts" import { srgbColorValueToHslColorValue, srgbColorValueToHsvColorValue } from "./convert.ts" import { internalGetStableSrgbColorValueByString, internalNormalizeHslColorValue, internalNormalizeHsvColorValue, internalNormalizeLinearRgbColorValue, internalNormalizeSrgbColorValue, } from "./internal.ts" /** * @description 构造一个 Linear RGB 颜色值对象。 * * @example * ``` * // Expect: { red: 0.2, green: 0.4, blue: 0.6, alpha: 1 } * const example1 = createLinearRgbColorValue({ red: 0.2, green: 0.4, blue: 0.6, alpha: 1 }) * * // Expect: { red: -0.25, green: 0.4, blue: 1.2, alpha: 0.5 } * const example2 = createLinearRgbColorValue({ red: -0.25, green: 0.4, blue: 1.2, alpha: 0.5 }) * ``` */ export const createLinearRgbColorValue = (color: LinearRgbColorValue): LinearRgbColorValue => { return internalNormalizeLinearRgbColorValue(color) } /** * @description 构造一个 sRGB 颜色值对象。 * * @example * ``` * // Expect: { red: 51, green: 102, blue: 153, alpha: 1 } * const example1 = createSrgbColorValue({ red: 51, green: 102, blue: 153, alpha: 1 }) * * // Expect: { red: 255, green: 255, blue: 255, alpha: 0.5 } * const example2 = createSrgbColorValue({ red: 255, green: 255, blue: 255, alpha: 0.5 }) * ``` */ export const createSrgbColorValue = (color: SrgbColorValue): SrgbColorValue => { return internalNormalizeSrgbColorValue(color) } /** * @description 构造一个 HSL 颜色值对象。 * * @example * ``` * // Expect: { hue: 330, saturation: 0.5, lightness: 0.4, alpha: 1 } * const example1 = createHslColorValue({ hue: -30, saturation: 0.5, lightness: 0.4, alpha: 1 }) * * // Expect: { hue: 0, saturation: 0, lightness: 1, alpha: 0.5 } * const example2 = createHslColorValue({ hue: 360, saturation: 0, lightness: 1, alpha: 0.5 }) * ``` */ export const createHslColorValue = (color: HslColorValue): HslColorValue => { return internalNormalizeHslColorValue(color) } /** * @description 构造一个 HSV 颜色值对象。 * * @example * ``` * // Expect: { hue: 330, saturation: 0.5, value: 0.6, alpha: 1 } * const example1 = createHsvColorValue({ hue: -30, saturation: 0.5, value: 0.6, alpha: 1 }) * * // Expect: { hue: 30, saturation: 0.5, value: 0.6, alpha: 1 } * const example2 = createHsvColorValue({ hue: 390, saturation: 0.5, value: 0.6, alpha: 1 }) * ``` */ export const createHsvColorValue = (color: HsvColorValue): HsvColorValue => { return internalNormalizeHsvColorValue(color) } /** * @description 根据稳定字符串构造 sRGB 颜色值。 * * @example * ``` * // Expect: { red: 254, green: 192, blue: 46, alpha: 1 } * const example1 = createSrgbColorValueByString("mobius") * * // Expect: { red: 101, green: 227, blue: 144, alpha: 1 } * const example2 = createSrgbColorValueByString("planet") * ``` */ export const createSrgbColorValueByString = (input: string): SrgbColorValue => { return internalGetStableSrgbColorValueByString(input) } /** * @description 根据稳定字符串构造 HSL 颜色值。 * * @example * ``` * // Expect: { hue: 42.992126, saturation: 0.991597, lightness: 0.588235, alpha: 1 } * const example1 = createHslColorValueByString("mobius") * * // Expect: { hue: 139.777778, saturation: 0.71831, lightness: 0.643137, alpha: 1 } * const example2 = createHslColorValueByString("planet") * ``` */ export const createHslColorValueByString = (input: string): HslColorValue => { return srgbColorValueToHslColorValue(createSrgbColorValueByString(input)) } /** * @description 根据稳定字符串构造 HSV 颜色值。 * * @example * ``` * // Expect: { hue: 42.992126, saturation: 0.818898, value: 0.996078, alpha: 1 } * const example1 = createHsvColorValueByString("mobius") * * // Expect: { hue: 139.777778, saturation: 0.555066, value: 0.890196, alpha: 1 } * const example2 = createHsvColorValueByString("planet") * ``` */ export const createHsvColorValueByString = (input: string): HsvColorValue => { return srgbColorValueToHsvColorValue(createSrgbColorValueByString(input)) }