diff --git a/README.md b/README.md index c73b769..81968ba 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # chalk-advanced -Chalk-advanced is my own version of chalk I made for fun. +Chalk-advanced is a library that allows you to color your console output. [![size](https://img.shields.io/github/repo-size/mezotv/chalk-advanced?color=red&label=SIZE)](https://www.npmjs.com/package/chalk-advanced) @@ -41,6 +41,12 @@ const { red } = require('chalk-advanced'); console.log(red('Hello World!')); ``` +```js +const { rgb } = require('chalk-advanced'); + +console.log(rgb(250, 30, 107, "Hello World!")); +``` + ## Colors - **`black(text)`**: @@ -81,6 +87,9 @@ console.log(red('Hello World!')); - **`inverse(text)`**: - **`hide(text)`**: - **`strikethrough(text)`**: +- **`hex(code, text)`**: +- **`hsl(h, s, l, text)`**: +- **`rgb(r, g, b, text)`**: ## Contributors diff --git a/index.d.ts b/index.d.ts index 6308be9..2973234 100644 --- a/index.d.ts +++ b/index.d.ts @@ -41,6 +41,9 @@ export { inverse, hide, strikethrough, + hex, + rgb, + hsl }; declare function black(text: string): string; @@ -81,6 +84,9 @@ declare function underline(text: string): string; declare function inverse(text: string): string; declare function hide(text: string): string; declare function strikethrough(text: string): string; +declare function hex(code: string, text: string): string; +declare function rgb(r: number, g: number, b: number, text: string): string; +declare function hsl(h: number, s: number, l: number, text: string): string; declare class ChalkAdvanced { black(text: string): string; @@ -121,4 +127,7 @@ declare class ChalkAdvanced { inverse(text: string): string; hide(text: string): string; strikethrough(text: string): string; + hex(code: string, text: string): string; + rgb(r: number, g: number, b: number, text: string): string; + hsl(h: number, s: number, l: number, text: string): string; } diff --git a/index.js b/index.js index 80d0f1e..91edc23 100644 --- a/index.js +++ b/index.js @@ -40,6 +40,10 @@ const { strikethrough } = require('./src/ChalkAdvanced'); +const { hex } = require("./src/components/hex"); +const { rgb } = require("./src/components/rgb"); +const { hsl } = require("./src/components/hsl"); + module.exports = { ChalkAdvanced: require("./src/ChalkAdvanced"), black: black, @@ -79,5 +83,8 @@ module.exports = { underline: underline, inverse: inverse, hide: hide, - strikethrough: strikethrough + strikethrough: strikethrough, + hex: hex, + rgb: rgb, + hsl: hsl } \ No newline at end of file diff --git a/package.json b/package.json index 6b66ab2..f04ef29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chalk-advanced", - "version": "1.0.5", + "version": "1.0.6", "description": "Chalk-advanced is a library that allows you to color your console output.", "main": "index.js", "scripts": { diff --git a/src/components/hex.js b/src/components/hex.js new file mode 100644 index 0000000..884bbdf --- /dev/null +++ b/src/components/hex.js @@ -0,0 +1,12 @@ +function hex(code, text) { + var aRgbHex = code.match(/.{1,2}/g); + var aRgb = [ + parseInt(aRgbHex[0], 16), + parseInt(aRgbHex[1], 16), + parseInt(aRgbHex[2], 16) + ]; + + return `\x1b[38;2;${aRgb[0]};${aRgb[1]};${aRgb[2]}m${text}\x1b[0m`; +} + +exports.hex = hex; \ No newline at end of file diff --git a/src/components/hsl.js b/src/components/hsl.js new file mode 100644 index 0000000..fed3111 --- /dev/null +++ b/src/components/hsl.js @@ -0,0 +1,16 @@ +const { hex } = require("./hex"); + +function hsl(h, s, l, text) { + l /= 100; + const a = s * Math.min(l, 1 - l) / 100; + const f = n => { + const k = (n + h / 30) % 12; + const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return Math.round(255 * color).toString(16).padStart(2, '0'); + }; + const hexValue = `${f(0)}${f(8)}${f(4)}`; + + return hex(hexValue, text); +} + +exports.hsl = hsl; \ No newline at end of file diff --git a/src/components/rgb.js b/src/components/rgb.js new file mode 100644 index 0000000..6ca980a --- /dev/null +++ b/src/components/rgb.js @@ -0,0 +1,5 @@ +function rgb(r, g, b, text) { + return `\x1b[38;2;${r};${g};${b}m${text}\x1b[0m` +} + +exports.rgb = rgb; \ No newline at end of file