import { IRGBANomral } from "./readColor" export enum AlphaType { force, auto, none, } export function toHex(color: IRGBANomral, options?: { alphaFirst?: boolean; alphaType?: AlphaType }): string { let arr = [color.r, color.g, color.b] // AlphaType.force if (options?.alphaType === AlphaType.force) { arr.push(Math.round((color.alpha ?? 1) * 255)) } // AlphaType.none else if (options?.alphaType === AlphaType.none) { } // AlphaType.auto else { if (color.alpha != undefined && color.alpha != 1) { arr.push(Math.round(color.alpha * 255)) } } let hex = [] for (let i = 0; i < arr.length; i++) { let num = Math.round(arr[i]) let char = num.toString(16) if (char.length == 1) char = "0" + char if (i == 3) { if (options?.alphaFirst) { hex.unshift(char) } else { hex.push(char) } } else { hex.push(char) } } return "#" + hex.join("") }