Skip to content

Commit

Permalink
fix(colors): Added more colorStart options
Browse files Browse the repository at this point in the history
  • Loading branch information
jamonholmgren committed Oct 16, 2023
1 parent aa69ac4 commit aeb181c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export declare const italic: (text: string) => `\u001B[${number}${string}\u001B[
export declare const underline: (text: string) => `\u001B[${number}${string}\u001B[${number}`;
export declare const inverse: (text: string) => `\u001B[${number}${string}\u001B[${number}`;
export declare const colorStart: <Col extends number>(color: Col) => `\u001B[${Col}m`;
export declare const bgify: <Col extends number>(color: Col) => number;
export declare const colorEnd: "\u001B[39m";
export declare const bgColorEnd: "\u001B[49m";
export declare const color: <Col extends number>(col: Col) => <Txt extends string>(text: Txt) => `\u001B[${Col}m${Txt extends `${infer First}\u001B[39m${infer Rest}` ? `${First}\u001B[${Col}m${Rest}` : Txt}\u001B[39m`;
Expand All @@ -99,8 +98,12 @@ export declare const hexToRgb: <Hex extends string>(hex: Hex) => readonly [
number
];
export declare const colorHex: <Hex extends string>(hex: Hex) => <Txt extends string>(text: Txt) => `\u001B[38;2;${number};${number};${number}m${Txt extends `${infer First}\u001B[39m${infer Rest}` ? `${First}\u001B[38;2;${number};${number};${number}m${Rest}` : Txt}\u001B[39m`;
export declare const colorHexStart: <Hex extends string>(hex: Hex) => `\u001B[38;2;${number};${number};${number}m`;
export declare const bgColorHex: <Hex extends string>(hex: Hex) => <Txt extends string>(text: Txt) => `\u001B[48;2;${number};${number};${number}m${Txt extends `${infer First}\u001B[49m${infer Rest}` ? `${First}\u001B[48;2;${number};${number};${number}m${Rest}` : Txt}\u001B[49m`;
export declare const bgColorHexStart: <Hex extends string>(hex: Hex) => `\u001B[48;2;${number};${number};${number}m`;
export declare const colorRGBStart: <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => `\u001B[38;2;${R};${G};${B}m`;
export declare const colorRGB: <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => <Txt extends string>(text: Txt) => `\u001B[38;2;${R};${G};${B}m${Txt extends `${infer First}\u001B[39m${infer Rest}` ? `${First}\u001B[38;2;${R};${G};${B}m${Rest}` : Txt}\u001B[39m`;
export declare const bgColorRGBStart: <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => `\u001B[48;2;${R};${G};${B}m`;
export declare const bgColorRGB: <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => <Txt extends string>(text: Txt) => `\u001B[48;2;${R};${G};${B}m${Txt extends `${infer First}\u001B[49m${infer Rest}` ? `${First}\u001B[48;2;${R};${G};${B}m${Rest}` : Txt}\u001B[49m`;
export declare const ansiColors: {
white: number;
Expand Down
29 changes: 19 additions & 10 deletions src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const inverse = style(7, 27)

export const colorStart = <Col extends number>(color: Col) => `${ESC}${color}m` as const

export const bgify = <Col extends number>(color: Col) => color + (10 as const)

export const colorEnd = `${ESC}39m` as const
export const bgColorEnd = `${ESC}49m` as const

Expand Down Expand Up @@ -55,28 +53,39 @@ export const colorHex = <Hex extends string>(hex: Hex) => {
return colorRGB(...hexToRgb(hex.replace("#", "")))
}

export const bgColorHex = <Hex extends string>(hex: Hex) => {
const rgb = hexToRgb(replace(hex, "#", ""))
const r = rgb[0]
const g = rgb[1]
const b = rgb[2]
export const colorHexStart = <Hex extends string>(hex: Hex) => {
const [r, g, b] = hexToRgb(replace(hex, "#", ""))
return colorRGBStart(r, g, b)
}

// convert hex to rbg to ansi (strip any # prefix)
export const bgColorHex = <Hex extends string>(hex: Hex) => {
const [r, g, b] = hexToRgb(replace(hex, "#", ""))
return bgColorRGB(r, g, b)
}

export const bgColorHexStart = <Hex extends string>(hex: Hex) => {
const [r, g, b] = hexToRgb(replace(hex, "#", ""))
return bgColorRGBStart(r, g, b)
}

export const colorRGBStart = <R extends number, G extends number, B extends number>(r: R, g: G, b: B) =>
`${ESC}38;2;${r};${g};${b}m` as const

// returns escape codes wrapped around text for a given rgb color
export const colorRGB = <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => {
const startCode = `${ESC}38;2;${r};${g};${b}m` as const
const startCode = colorRGBStart(r, g, b)
return <Txt extends string>(text: Txt) => {
// first replace any existing reset color with this color
const newText = replace(text, colorEnd, startCode)
return `${startCode}${newText}${colorEnd}` as const
}
}

export const bgColorRGBStart = <R extends number, G extends number, B extends number>(r: R, g: G, b: B) =>
`${ESC}48;2;${r};${g};${b}m` as const

export const bgColorRGB = <R extends number, G extends number, B extends number>(r: R, g: G, b: B) => {
const startCode = `${ESC}48;2;${r};${g};${b}m` as const
const startCode = bgColorRGBStart(r, g, b)
return <Txt extends string>(text: Txt) => {
// first replace any existing reset color with this color
const newText = replace(text, bgColorEnd, startCode)
Expand Down

0 comments on commit aeb181c

Please sign in to comment.