diff --git a/lib/types.d.ts b/lib/types.d.ts index eb98d4aa..e21d4afd 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -576,9 +576,5 @@ export type ColorFamily = | "purple" | "gray"; -export type Swatch = T extends Tailwind - ? V extends ScaleValues ? `${T}[${V}]` - : Array<`all[${V | "500"}]`> - : Array; export type LightnessOptions = { amount?: number; darken?: boolean }; diff --git a/lib/utils.ts b/lib/utils.ts index 06f5fa34..1ffbe4ec 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -49,9 +49,9 @@ import type { /** * - * Returns the color token's alpha channel value. + * Sets and retrieves the color token's alpha (or opacity). * - * If the the `amount` parameter is passed in, it sets the color token's alpha channel with the `amount` specified + * If the the `amount` argument is passed in, it sets the color token's alpha channel with the `amount` specified * and returns the color as a hex string. * * :::tip @@ -74,7 +74,7 @@ import type { console.log(alpha('#a1bd2f0d')) // 0.050980392156862744 -// Setting the alpha +// Setting the alpha let myColor = alpha('b2c3f1', 0.5) @@ -91,65 +91,87 @@ function alpha( // @ts-ignore: let alphaChannel: number; + // @ts-ignore: + let len: number, + hasMode: string | undefined, + hasAlpha: boolean, + alphaIdx: number + + // @ts-ignore: + + if (isArray(color)) { - alphaChannel = (color as ColorTuple).filter((channel) => + + len = color?.length + // @ts-ignore: + hasMode = (color as ColorTuple).find((c) => + eq(typeof c, "string") + ) + hasAlpha = (color as ColorTuple).filter((channel) => eq(typeof channel, "number") ) + // if the length is 4 then the alpha is arr.length - 1 .length === - 4 && - color[(color as ColorTuple)?.length - 1] || - 1; - } else if (eq(typeof color, "string")) { + 4 + + + alphaIdx = hasAlpha ? len - 1 : hasMode ? 4 : 3 + + + alphaChannel = color[alphaIdx] + } + + + + + if (eq(typeof color, "string")) alphaChannel = ( gte((color as ColorTuple)?.length, 8) && // @ts-ignore: - not(colorsNamed?.color?.toLowerCase()) + !colorsNamed[(color as string).toLowerCase()] ) ? Number.parseInt( (color as string)?.slice((color as string)?.length - 2), 16, - ) + ) / 255 : 1; - } // @ts-ignore: - else if (typeof color === "object" && !color?.length) { + // @ts-ignore: + if (typeof color === "object" && !len) // @ts-ignore: alphaChannel = color?.alpha; - } - if (!amount) { + + + + // if amount is undefined, return the alpha channel + if (typeof amount == 'undefined') // @ts-ignore: return alphaChannel && alphaChannel || 1; - } - amount = ( - // @ts-ignore: - ((typeof amount !== "number") && exprParser(alphaChannel, amount)) || - // @ts-ignore: - ((inRange(amount, 0, 1) && amount) || give(amount, 100)) - ); - if (isArray(color)) { - // Get the alpha index - color[ - ( - ( - (color as ColorTuple).length === 5 || - ( - (color[0] !== "string") && - ((color as ColorTuple)?.length === 4) - ) - ) && - (color as ColorTuple).length - 1 - ) || 3 - ] = amount; + + switch (typeof amount) { + case 'number': + // @ts-ignore: + amount = (inRange(amount, 0, 1) && amount) || give(amount, 100) + break; + // @ts-ignore: + case 'string': amount = exprParser(alphaChannel, amount) + } + + if (isArray(color)) + // @ts-ignore: + color[alphaIdx] = amount; + + // @ts-ignore: - if (eq(typeof color, "object") && !color?.length) { + if (eq(typeof color, "object") && !len) // @ts-ignore: color.alpha = amount; - } else { + if (typeof color == 'number' || typeof color == 'string') { const colorObject = token(color, { kind: "obj" }); // @ts-ignore: colorObject.alpha = amount; @@ -190,26 +212,18 @@ function mc(modeChannel = "lch.h") { const [mode, channel] = modeChannel.split("."); // @ts-ignore: - let colorObject = token(color, { targetMode: mode, kind: "obj" }); + const colorObject = token(color, { targetMode: mode, kind: "obj" }); // @ts-ignore: const currentChannel: number = colorObject[channel]; - if (value) { - if (eq(typeof value, "number")) { - // @ts-ignore: - colorObject[channel] = value; - } else if (eq(typeof value, "string")) { - // @ts-ignore: - colorObject = exprParser(colorObject[channel], value); - } else { - throw Error( - `${typeof value} ${value} is not a valid value for a color token`, - ); - } - } - + if (typeof value != 'undefined') + // @ts-ignore: + colorObject[channel] = typeof value === 'number' && value || exprParser(colorObject[channel], value); // @ts-ignore: return value && colorObject || currentChannel; + + + }; } @@ -299,14 +313,14 @@ console.log(brighten('blue', 0.3)); */ function lightness( - color?: ColorToken, - options: LightnessOptions = { - amount: 0.1, - darken: false, - }, + color: ColorToken, + options?: LightnessOptions ): ColorToken { - const { amount, darken } = options; + let { amount, darken } = options || {} as LightnessOptions + + amount = amount || 0.1 + darken = darken || false const f = () => { const colorObject = token(color, { kind: "obj", targetMode: "lab" }); if (amount) diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 5dc4e62e..84c63262 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -5,7 +5,7 @@ import runner, { type Spec } from "./runner.ts"; -const str = '#ffc3b0ff', arr = ['rgb', 0.4, 0.3, 0.1, 0.7], obj = { r: 0.2, g: 0.4, b: 0.5, mode: 'rgb' }, +const str = '#ffc3b03b', arr = ['rgb', 0.4, 0.3, 0.1, 0.7], obj = { r: 0.2, g: 0.4, b: 0.5, mode: 'rgb' }, fn_mc = (a: string, b: string, c: number | string) => mc(a)(b, c) const specs: Spec[] = [{ diff --git a/www/bun.lockb b/www/bun.lockb deleted file mode 100755 index 97fc622b..00000000 Binary files a/www/bun.lockb and /dev/null differ