diff options
author | Oliver Lenehan <sunsetkookaburra+git@outlook.com.au> | 2020-05-21 00:29:59 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-20 10:29:59 -0400 |
commit | 662eb8f8c981565950d35cdd1c9ca6a67eaef8f2 (patch) | |
tree | 2aaf3fcdc11c0f8968197edc342e15f8cb194fed /std/fmt/colors.ts | |
parent | f5c0188b5edf9451aca42347dd4a48708fefce23 (diff) |
feat(std/fmt): rgb24 and bgRgb24 can use numbers for color (#5198)
Diffstat (limited to 'std/fmt/colors.ts')
-rw-r--r-- | std/fmt/colors.ts | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/std/fmt/colors.ts b/std/fmt/colors.ts index 832f3b164..6a06af20e 100644 --- a/std/fmt/colors.ts +++ b/std/fmt/colors.ts @@ -172,8 +172,22 @@ export function bgRgb8(str: string, color: number): string { return run(str, code([48, 5, clampAndTruncate(color)], 49)); } -/** Set text color using 24bit rgb. */ -export function rgb24(str: string, color: Rgb): string { +/** Set text color using 24bit rgb. + * `color` can be a number in range `0x000000` to `0xffffff` or + * an `Rgb`. + * + * To produce the color magenta: + * + * rgba24("foo", 0xff00ff); + * rgba24("foo", {r: 255, g: 0, b: 255}); + */ +export function rgb24(str: string, color: number | Rgb): string { + if (typeof color === "number") { + return run( + str, + code([38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 39) + ); + } return run( str, code( @@ -189,8 +203,22 @@ export function rgb24(str: string, color: Rgb): string { ); } -/** Set background color using 24bit rgb. */ -export function bgRgb24(str: string, color: Rgb): string { +/** Set background color using 24bit rgb. + * `color` can be a number in range `0x000000` to `0xffffff` or + * an `Rgb`. + * + * To produce the color magenta: + * + * bgRgba24("foo", 0xff00ff); + * bgRgba24("foo", {r: 255, g: 0, b: 255}); + */ +export function bgRgb24(str: string, color: number | Rgb): string { + if (typeof color === "number") { + return run( + str, + code([48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 49) + ); + } return run( str, code( |