Skip to content

Commit

Permalink
fix: alpha() now sets the alpha value on arrays as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlwizard committed Dec 19, 2024
1 parent a1ee1e6 commit d369a18
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,34 @@ console.log(myColor)
// #b2c3f180
*/

// ! fix bug when setting alpha value to array'./
function alpha<Amount>(
color: ColorToken = "cyan",
amount: Amount | undefined = undefined,
): Amount extends undefined ? number : ColorToken {
// @ts-ignore:
let alphaChannel: number;

// @ts-ignore:
let len: number,
let alphaChannel: number,
len: number,
hasMode: string | undefined,
hasAlpha: boolean,
alphaIdx: number

// @ts-ignore:


if (isArray(color)) {

len = color?.length
if (isArray(color))
// @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


alphaIdx = hasAlpha ? len - 1 : hasMode ? 4 : 3
[len, hasMode, hasAlpha, alphaIdx, alphaChannel] =
[color?.length, (color as ColorTuple).find((c) =>
eq(typeof c, "string")
),
(color as ColorTuple).filter((channel) =>
eq(typeof channel, "number")
).length === 4,

// @ts-ignore:

alphaChannel = color[alphaIdx]
}

hasAlpha ? len - 1 : hasMode ? 4 : 3, color[alphaIdx]
];



Expand Down

0 comments on commit d369a18

Please sign in to comment.