import { IRGBANomral } from "./readColor" import { AlphaType, toHex } from "./toHex" import { round } from "../../math/funcs/round" // @ts-ignore import IchiColor from "../lib/ichiColor.js" export type ColorFormatType = | "hex" | "ahex" | "rgba" | "rgb" | "rgb2" | "hexa" | "hexOnly" | "swift" | "objective-c" | "hsl" | "hwb" | "hsla" | "hwba" export enum ColorFormat { hex = "hex", hexa = "hexa", hexOnly = "hexOnly", ahex = "ahex", rgba = "rgba", rgb = "rgb", rgb2 = "rgb2", swift = "swift", objectiveC = "objective-c", hsl = "hsl", hsla = "hsla", hwb = "hwb", hwba = "hwba", } let ichiColor!: any export function writeColor(color: IRGBANomral, format?: ColorFormatType) { if (!ichiColor) ichiColor = IchiColor() if (format == ColorFormat.hex) { return toHex(color, { alphaType: AlphaType.auto }) } if (format == ColorFormat.hexOnly) { return toHex(color, { alphaType: AlphaType.none }) } if (format == ColorFormat.hexa) { return toHex(color, { alphaType: AlphaType.force }) } if (format == ColorFormat.ahex) { return toHex(color, { alphaFirst: true, alphaType: AlphaType.force }) } if (format == ColorFormat.rgba) { return `rgba(${round(color.r)}, ${round(color.g)}, ${round(color.b)}, ${round(color.alpha ?? 1, 2)})` } if (format == ColorFormat.rgb) { return `rgb(${round(color.r)}, ${round(color.g)}, ${round(color.b)})` } if (format == ColorFormat.rgb2) { return `rgb(${round(color.r)} ${round(color.g)} ${round(color.b)}${ color.alpha != undefined ? " / " + Math.round(color.alpha * 100) + "%" : "" })` } if (format == ColorFormat.swift) { let r = round(color.r / 255, 3) let g = round(color.g / 255, 3) let b = round(color.b / 255, 3) let a = round(color.alpha ?? 1, 2) return `red: ${r}, green: ${g}, blue: ${b}, alpha: ${a}` } if (format == ColorFormat.objectiveC) { let r = color.r.toFixed(1) let g = color.g.toFixed(1) let b = color.b.toFixed(1) let a = (color.alpha ?? 1).toFixed(1) return `[UIColor colorWithRed:${r}f/255.0f green:${g}f/255.0f blue:${b}f/255.0f alpha:${a}f]` } if (format == ColorFormat.hsl) { ichiColor.set(color) let hsl: any = ichiColor.hsl if (ichiColor.alpha == 1) { return `hsl(${round(hsl.h)}, ${round(hsl.s)}%, ${round(hsl.l)}%)` } else { return `hsl(${round(hsl.h)}, ${round(hsl.s)}%, ${round(hsl.l)}%, ${round(ichiColor.alpha, 2)})` } } if (format == ColorFormat.hwb) { ichiColor.set(color) let hwb: any = ichiColor.hwb if (ichiColor.alpha == 1) { return `hwb(${round(hwb.h)}, ${round(hwb.w)}%, ${round(hwb.b)}%)` } else { return `hwb(${round(hwb.h)}, ${round(hwb.w)}%, ${round(hwb.b)}% , ${round(ichiColor.alpha, 2)})` } } }