From 4e4925881b1dbf9b7e447fe1bfbdf67fda48bc94 Mon Sep 17 00:00:00 2001 From: xmlwizard Date: Sun, 8 Dec 2024 22:30:28 +0200 Subject: [PATCH] fix: alpha now fetches the alpha channel correctly from arrays --- lib/utils.ts | 32 +++++++++++++++------------- tests/utils.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index e2f1a6be..a84dea1c 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -86,6 +86,8 @@ console.log(myColor) // #b2c3f180 */ + +// ! fix bug when setting alpha value to array'./ function alpha( color: ColorToken = "cyan", amount: Amount | undefined = undefined, @@ -94,15 +96,15 @@ function alpha( let alphaChannel: number; if (isArray(color)) - alphaChannel = eq( + alphaChannel = (color as ColorTuple).filter((channel) => eq(typeof channel, "number") ) - .length, - 4, - ) - ? color[(color as ColorTuple)?.length - 1] - : 1; + .length === + 4 + + && color[(color as ColorTuple)?.length - 1] + || 1; else if (eq(typeof color, "string")) alphaChannel = ( gte((color as ColorTuple)?.length, 8) && @@ -114,14 +116,16 @@ function alpha( 16, ) : 1; - else if (eq(typeof color, "object")) + + // @ts-ignore: + else if (typeof color === "object" && !color?.length) // @ts-ignore: alphaChannel = color?.alpha; if (!amount) // @ts-ignore: - return alphaChannel ? alphaChannel : 1; + return alphaChannel && alphaChannel || 1; amount = ( // @ts-ignore: @@ -136,18 +140,18 @@ function alpha( color[ ( ( - eq((color as ColorTuple).length, 5) || + (color as ColorTuple).length === 5 || ( - neq(color[0], "string") && - eq((color as ColorTuple)?.length, 4) + (color[0] !== "string") && + ((color as ColorTuple)?.length === 4) ) ) && - take((color as ColorTuple).length, 1) + (color as ColorTuple).length - 1 ) || 3 ] = amount; - - if (eq(typeof color, "object")) + // @ts-ignore: + if (eq(typeof color, "object") && !color?.length) // @ts-ignore: color.alpha = amount; else { diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 1a2dfc71..4402c723 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -1,11 +1,14 @@ -import { mc, token } from "../lib/utils.ts"; +import { alpha, mc, token } from "../lib/utils.ts"; import runner, { type Spec } from "./runner.ts"; -const str = '#ffc3003f', 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) => mc(a)(b) +const str = '#ffc3003f', 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[] = [{ + + // ? token() description: "converts an object to a number", callback: token, params: [obj, { kind: 'num', }] @@ -37,15 +40,55 @@ const specs: Spec[] = [{ }, -// modeChannel tests +// ? mc() +// * getting { description: 'gets the channel value of a color object', callback: fn_mc, - params: ['lch.l', obj] + params: ['lch.c', obj] }, { + description: 'gets the channel value of color array', + callback: fn_mc, params: ['lch.c', arr] +}, +{ + description: 'gets the channel value of color string', + callback: fn_mc, params: ['lch.c', str] +}, +// * setting + +{ description: 'sets the channel value of a color object', callback: fn_mc, params: ['lch.h', obj, '*0.2'] +}, + + + +{ + description: 'sets the channel value of a color array', + callback: fn_mc, + params: ['lch.h', arr, 20] +}, + +// ? alpha() +// * setting +{ + description: 'sets the alpha channel value on a color object', callback: alpha, params: [obj, 0.05] +}, { + description: 'sets the alpha channel value on a color string', callback: alpha, params: [str, 0.05] +}, { + description: 'sets the alpha channel value on a color array', callback: alpha, params: [arr, 0.05] +}, + +// * getting +{ + description: 'gets the alpha channel value from a color object', callback: alpha, params: [obj] +}, { + description: 'gets the alpha channel value from a color array', + callback: alpha, params: [arr] +}, { + description: 'gets the alpha channel value from a color string', + callback: alpha, params: [str] } ];