Skip to content

Commit

Permalink
fixed bug in getSrcMode. Now returns values as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
prjctimg committed Nov 13, 2024
1 parent f35042c commit 3ad3765
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,9 @@ function filteredColl(fact: Factor, cb: unknown) {
function getSrcMode(c: ColorToken): Colorspaces {



// @ts-ignore
return and(isArray(c), neq(typeof c[0], "number")) ? c[0] : eq(typeof 'object', c) ? c?.mode : 'rgb'
return and(isArray(c), neq(typeof c[0], "number")) ? c[0] : eq(typeof c, 'object') ? c?.mode : 'rgb'
}

const ctrst = (a: unknown) => (b: unknown) =>
Expand Down
38 changes: 23 additions & 15 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ function lightness(
function token(
color: ColorToken = 'cyan',
options: TokenOptions = {
srcMode: undefined,

normalizeRgb: true,
numType: undefined,
omitMode: false,
omitAlpha: false
omitAlpha: false, kind: 'str'
}
): ColorToken {

Expand All @@ -399,18 +399,18 @@ function token(
numType,
omitAlpha,
normalizeRgb,
} = options
} = options;


console.log(options)

srcMode = srcMode ?
srcMode
: getSrcMode(color as ColorToken);
console.log(srcMode)



// TODO: We want to parse color tokens from different types
//

/**
* An array of channel keys from the source colorspace. If undefined it defaults to 'rgb'
*/
Expand All @@ -427,29 +427,37 @@ function token(
const alphaValue = alpha(color);
let result = {};



// Get the channels from passed in color token
// if the color token is a string or number we just convert it to an object

if (isArray(color)) {
// @ts-ignore:
srcChannelValues = (color as ColorTuple).filter((a) =>
eq(typeof a, "number")
);
srcChannelValues = (color as ColorTuple)
.filter((a) => eq(typeof a, "number")
);
}
if (eq(typeof color, "object")) {

if (eq(typeof color, "object"))
// @ts-ignore:
srcChannelValues = srcChannels.map((a) => color[a]);
}
if (eq(typeof color, "string")) {

if (eq(typeof color, "string"))
// @ts-ignore:
result = eq(typeof color, "number")
? num2c()
: parseToken(c2str(), "rgb");
}

// @ts-ignore:
if (srcChannelValues) {
if (srcChannelValues)
for (const channel of srcChannels) {
// @ts-ignore:
result[channel] = srcChannelValues[srcChannels.indexOf(channel)];
}
}
console.log(srcChannels, srcChannelValues)



function parseToken(col: unknown, mode?: unknown) {
// @ts-ignore:
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { token } from './lib/utils.ts'

console.log(token())
console.log(token({ l: 5, c: 45, h: 87, mode: 'lch' }))

0 comments on commit 3ad3765

Please sign in to comment.