Skip to content

Commit

Permalink
feat: rgb-ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
const-void committed Aug 13, 2024
1 parent 7e053b4 commit 2c960e1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $ npx @rose-pine/build --help
| hex-ns | `ebbcba` |
| rgb | `235, 188, 186` |
| rgb-ns | `235 188 186` |
| rgb-ansi | `235;188;186` |
| rgb-array | `[235, 188, 186]` |
| rgb-function | `rgb(235, 188, 186)` |
| hsl | `2, 55%, 83%` |
Expand Down
1 change: 1 addition & 0 deletions source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Config = {
| "hex-ns"
| "rgb"
| "rgb-ns"
| "rgb-ansi"
| "rgb-array"
| "rgb-function"
| "hsl"
Expand Down
6 changes: 6 additions & 0 deletions source/utils/format-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ export const formatColor = (
const formattedRgb = rgb.join(", ");
const formattedHsl = `${h!}, ${s!}%, ${l!}%${hsl[3] ? `, ${hsl[3]}` : ""}`;

//ansi true-color strings cannot have spaces
if ( format == 'rgb-ansi' ) {
stripSpaces = true
}

const formats = {
hex: `#${hex}`,
"hex-ns": hex,
rgb: formattedRgb,
"rgb-ns": formattedRgb.replaceAll(",", ""),
"rgb-ansi": formattedRgb.replaceAll(",", ";"),
"rgb-array": `[${formattedRgb}]`,
"rgb-function": `rgb${rgb[3] ? "a" : ""}(${formattedRgb})`,
hsl: formattedHsl,
Expand Down
4 changes: 4 additions & 0 deletions test/format-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ test("color formats", (t) => {
t.is(formatColor(testColor, "hex-ns", true), "ebbcba");
t.is(formatColor(testColor, "rgb"), "235, 188, 186");
t.is(formatColor(testColor, "rgb-ns"), "235 188 186");
t.is(formatColor(testColor, "rgb-ansi"), "235;188;186");
t.is(formatColor(testColor, "rgb-array"), "[235, 188, 186]");
t.is(formatColor(testColor, "rgb-function"), "rgb(235, 188, 186)");
t.is(formatColor(testColor, "rgb", true), "235,188,186");
t.is(formatColor(testColor, "rgb-ns", true), "235188186");
t.is(formatColor(testColor, "rgb-ansi", true), "235;188;186");
t.is(formatColor(testColor, "rgb-array", true), "[235,188,186]");
t.is(formatColor(testColor, "rgb-function", true), "rgb(235,188,186)");
t.is(formatColor(testColor, "hsl"), "2, 55%, 83%");
Expand All @@ -43,10 +45,12 @@ test("alpha color formats", (t) => {
t.is(formatColor(testColor, "hex-ns", true), "6e6a8614");
t.is(formatColor(testColor, "rgb"), "110, 106, 134, 0.08");
t.is(formatColor(testColor, "rgb-ns"), "110 106 134 0.08");
t.is(formatColor(testColor, "rgb-ansi"), "110;106;134;0.08");
t.is(formatColor(testColor, "rgb-array"), "[110, 106, 134, 0.08]");
t.is(formatColor(testColor, "rgb-function"), "rgba(110, 106, 134, 0.08)");
t.is(formatColor(testColor, "rgb", true), "110,106,134,0.08");
t.is(formatColor(testColor, "rgb-ns", true), "1101061340.08");
t.is(formatColor(testColor, "rgb-ansi", true), "110;106;134;0.08");
t.is(formatColor(testColor, "rgb-array", true), "[110,106,134,0.08]");
t.is(formatColor(testColor, "rgb-function", true), "rgba(110,106,134,0.08)");
t.is(formatColor(testColor, "hsl"), "249, 12%, 47%, 0.08");
Expand Down

0 comments on commit 2c960e1

Please sign in to comment.