Skip to content

Commit

Permalink
fix: alpha now fetches the alpha channel correctly from arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlwizard committed Dec 8, 2024
1 parent a511a5c commit 4e49258
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
32 changes: 18 additions & 14 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ console.log(myColor)
// #b2c3f180
*/

// ! fix bug when setting alpha value to array'./
function alpha<Amount>(
color: ColorToken = "cyan",
amount: Amount | undefined = undefined,
Expand All @@ -94,15 +96,15 @@ function alpha<Amount>(
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) &&
Expand All @@ -114,14 +116,16 @@ function alpha<Amount>(
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:
Expand All @@ -136,18 +140,18 @@ function alpha<Amount>(
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 {
Expand Down
51 changes: 47 additions & 4 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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', }]
Expand Down Expand Up @@ -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]
}
];

Expand Down

0 comments on commit 4e49258

Please sign in to comment.