Skip to content

Commit

Permalink
Add support for hsl, hex and rgb.
Browse files Browse the repository at this point in the history
Improved typing
Added hsl, hex and rgb support,
Fixed typos

Co-Authored-By: Marc <71778381+MarcWebDev@users.noreply.github.com>
  • Loading branch information
mezotv and MarcWebDev committed Nov 25, 2022
1 parent dab17be commit fbc1426
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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)`**:
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export {
inverse,
hide,
strikethrough,
hex,
rgb,
hsl
};

declare function black(text: string): string;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,5 +83,8 @@ module.exports = {
underline: underline,
inverse: inverse,
hide: hide,
strikethrough: strikethrough
strikethrough: strikethrough,
hex: hex,
rgb: rgb,
hsl: hsl
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 12 additions & 0 deletions src/components/hex.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions src/components/hsl.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions src/components/rgb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function rgb(r, g, b, text) {
return `\x1b[38;2;${r};${g};${b}m${text}\x1b[0m`
}

exports.rgb = rgb;

0 comments on commit fbc1426

Please sign in to comment.