[READ-ONLY] Mirror of https://github.com/probablykasper/colorboy-js. Easy terminal coloring for Node.js, macOS/Linux npmjs.com/package/colorboy
ansi cli color package terminal
0

Configure Feed

Select the types of activity you want to include in your feed.

Fixed rgb colors not working

Kasper (Sep 5, 2020, 7:56 PM +0200) 8f4f0dc2 d16f28b9

+73 -52
+22 -17
README.md
··· 13 13 console.log("Potato chips".cyan.underline.italic); 14 14 console.log("The Eden Project".color("#067CB6").bgColor([25, 25, 150]).bold); 15 15 ``` 16 - ![screenshot1](https://raw.githubusercontent.com/SpectralKH/colorboy/master/screenshot1.png) 16 + ![screenshot1](./screenshot1.png) 17 17 18 18 ### Colors 19 19 A color can be: ··· 39 39 40 40 ## Usage - Custom colors & styles 41 41 ```js 42 - let currentColor = "red"; 43 - require("colorboy") 44 - .addColor("greenish", { 45 - color: "#000000", 46 - bgColor: "#00FE7C", 47 - style: ["bold", "italic"], 48 - }) 49 - .addColorFunction("error", (color) => { 50 - return { 51 - color: color, 52 - bgColor: currentColor, 53 - } 54 - }) 55 - console.log("Unlike Pluto".greenish); 56 - console.log("Unlike Pluto".error("white")); 42 + let currentBgColor = 'red'; 43 + 44 + require('./index.js') 45 + .addDefaults() 46 + .addColor('greenish', { 47 + color: '#000000', 48 + bgColor: '#00FE7C', 49 + style: ['bold', 'italic'], 50 + }) 51 + .addColorFunction('error', (color) => { 52 + return { 53 + color: color, 54 + bgColor: currentBgColor, 55 + } 56 + }) 57 + console.log('Custom:'); 58 + console.log('Unlike Pluto'.greenish); 59 + console.log('Unlike Pluto'.error('white')); 60 + currentBgColor = 'cyan' 61 + console.log('Unlike Pluto'.error('black')); 57 62 ``` 58 - ![screenshot2](https://raw.githubusercontent.com/SpectralKH/colorboy/master/screenshot2.png) 63 + ![screenshot2](./screenshot2.png) 59 64 60 65 ### colorboy.addDefaults(functions, colors, styles) 61 66 Adds the default colorboy colors & styles. Takes three optional arguments, all true by default.
+21 -13
index.js
··· 1 1 function getColorType(color) { 2 - if (Array.isArray(color) && color.length == 1) color = color[0]; 3 - if (typeof color == "string") { 2 + if (Array.isArray(color) && color.length === 1) color = color[0]; 3 + if (typeof color === "string") { 4 4 if (color.startsWith("#")) return "hex"; 5 5 return "keyword"; 6 6 } else if (Array.isArray(color)) { 7 - if (color.length == 3) return "rgb"; 8 - if (color.length != 4) throw new Error("colorboy: invalid color. Array needs to have 3 or 4 arguments."); 9 - if (color[3] == "rgb") return "rgb"; 10 - if (color[3] == "hsl") return "hsl"; 11 - if (color[3] == "hsv") return "hsv"; 12 - if (color[3] == "hwb") return "hwb"; 7 + if (color.length === 3) return "rgb"; 8 + if (color.length !== 4) throw new Error("colorboy: invalid color. Array needs to have 3 or 4 arguments."); 9 + if (color[3] === "rgb") return "rgb"; 10 + if (color[3] === "hsl") return "hsl"; 11 + if (color[3] === "hsv") return "hsv"; 12 + if (color[3] === "hwb") return "hwb"; 13 13 throw new Error("colorboy: invalid color. Color type specified has to be rgb, hsl, hsv or hwb."); 14 14 } 15 15 } ··· 35 35 const chalk = require("chalk"); 36 36 function coloration(str, options = {}) { 37 37 let result = chalk; 38 - const color = options.color; 38 + let color = options.color; 39 39 const bgColor = options.bgColor; 40 40 const style = options.style; 41 41 if (color) { 42 42 const colorType = getColorType(color); 43 - result = result[colorType](color); 43 + if (Array.isArray(color)) { 44 + result = result[colorType](...color); 45 + } else { 46 + result = result[colorType](color); 47 + } 44 48 } 45 49 if (bgColor) { 46 50 const colorType = "bg"+capitalizeFirstLetter(getColorType(bgColor)); 47 - result = result[colorType](bgColor); 51 + if (Array.isArray(color)) { 52 + result = result[colorType](...bgColor); 53 + } else { 54 + result = result[colorType](bgColor); 55 + } 48 56 } 49 57 if (style) { 50 - if (typeof style == "string") { 58 + if (typeof style === "string") { 51 59 result = result[getStyleType(style)]; 52 60 } else if (Array.isArray(style)) { 53 61 for (let i = 0; i < style.length; i++) { ··· 71 79 addColor: (name, options) => { 72 80 Object.defineProperty(global.String.prototype, name, { 73 81 get: function() { 74 - if (typeof options == "function") options = options(this); 82 + if (typeof options === "function") options = options(this); 75 83 return coloration(this, options); 76 84 } 77 85 });
screenshot2.png

This is a binary file and will not be displayed.

+30 -22
test.js
··· 1 - // require("./index.js")(); 1 + let currentBgColor = 'red'; 2 2 3 - // screenshot1 4 - // require("./index.js").addDefaults(); 5 - // console.log("Globgogabgalab".red); 6 - // console.log("Potato chips".cyan.underline.italic); 7 - // console.log("The Eden Project".color("#067CB6").bgColor([25, 25, 150]).bold); 3 + require('./index.js') 4 + .addDefaults() 5 + .addColor('greenish', { 6 + color: '#000000', 7 + bgColor: '#00FE7C', 8 + style: ['bold', 'italic'], 9 + }) 10 + .addColorFunction('error', (color) => { 11 + return { 12 + color: color, 13 + bgColor: currentBgColor, 14 + } 15 + }) 8 16 9 - // screenshot2 10 - let currentColor = "red"; 11 - require("./index.js") 12 - .addColor("greenish", { 13 - color: "#000000", 14 - bgColor: "#00FE7C", 15 - style: ["bold", "italic"], 16 - }) 17 - .addColorFunction("error", (color) => { 18 - return { 19 - color: color, 20 - bgColor: currentColor, 21 - } 22 - }) 23 - console.log("Unlike Pluto".greenish); 24 - console.log("Unlike Pluto".error("white")); 17 + console.log('Custom:\n'); 18 + console.log('Unlike Pluto'.greenish); 19 + console.log('Unlike Pluto'.error('white')); 20 + currentBgColor = 'cyan' 21 + console.log('Unlike Pluto'.error('black')); 22 + 23 + console.log('\nDefaults:\n'); 24 + console.log('cyan underline italic'.cyan.underline.italic); 25 + console.log('color() keyword'.color('pink')); 26 + console.log('color() hex'.color('#bc5bd2')); 27 + console.log('color() rgb default'.color([188, 91, 210])); 28 + console.log('color() rgb'.color([188, 91, 210, 'rgb'])); 29 + console.log('color() hsl'.color([289, 57, 59, 'hsl'])); 30 + console.log('color() hwb'.color([289, 36, 18, 'hwb'])); 31 + 32 + console.log('\nDefaults:\n');