This is a small JavaScript pseudo-class that I use to quickly convert HEX to RGB colour format and RGB back to HEX and also create an array of gradient colours between two colours.
This guide is just a convenient place for me to store this reference for future use. I hope you can get some use out of it too.
Methods
- rgbToHex(r, g, b) – returns
String
– A hexadecimal colour. - hexToRgb(hex) – returns
[Number, Number, Number]
– A red, green and blue (RGB) array of numbers from 0-255 - generateColorGradient(colorStart, colorEnd, steps) – returns
[[Number, Number, Number], ...]
– An array of RGB colours by a set step.
The Code – Colory Class in JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/** * A small JavaScript Color Class used to: * 1. Convet RGB to HEX * 2. Convert HEX to RGB * 3. Generate a colour gradient between two colours. * * @author Scott Donald <scott@yagisanatode.com> * @see Documentation {@link https://yagisanatode.com/colory} * @license BSD 3-Clause * */ function colory(){ const publicAPI = {} /** * Converts rgb to hex. * If your rgb is in an array you can use the spread operator. * e.g. color().rgbToHex(...rgb) * @param {Number} r - red 3-digit color code. * @param {Number} g - green 3-digit color code. * @param {Number} b - blue 3-digit color code. * @returns {String} Hexadeciaml string */ publicAPI.rgbToHex = function(r, g, b){ const rgbValue = (r << 16) | (g << 8) | (b << 0); const hex = '#' + (0x1000000 + rgbValue).toString(16).slice(1); return hex } /** * Converts Hexadecimal colour to RGB * @param {String} hex * @returns {[Number, Number, Number]} RGB number set for target color. */ publicAPI.hexToRgb = function(hex){ // Remove the leading # if it's there hex = hex.replace(/^#/, ''); // Parse the hex values let colorInt = parseInt(hex, 16); let r = (colorInt >> 16) & 255; let g = (colorInt >> 8) & 255; let b = colorInt & 255; return [r,g,b] } /** * Generates a color gradient based on the start and * end colours. * @param {String|[Number, Number, Number]} colorStart - Start color as Hexadeciaml string or set of 3 RGB 3-digit number series. * @param {String|[Number, Number, Number]} colorEnd - Start color as Hexadeciaml string or set of 3 RGB 3-digit number series. * @param {Number} steps - total number of steps to break down into. * @returns {[Number, Number, Number]} RGB number set for target color. */ publicAPI.generateColorGradient = function(colorStart, colorEnd, steps){ if(typeof colorStart === "string"){ colorStart = this.hexToRgb(colorStart) } if(typeof colorEnd === "string"){ colorEnd = this.hexToRgb(colorEnd) } const colors = [] steps = steps - 1 for(let i = 0; i <= steps; i++){ const r = Math.round(colorStart[0] + ((colorEnd[0] - colorStart[0]) / steps) * i); const g = Math.round(colorStart[1] + ((colorEnd[1] - colorStart[1]) / steps) * i); const b = Math.round(colorStart[2] + ((colorEnd[2] - colorStart[2]) / steps) * i); colors.push([r, g, b]) } return colors } return publicAPI } |
Detailed Documentation
rgbToHex(r, g, b)
Converts RGB color (Red, Green Blue) to hexadecimal colour.
If your rgb is in an array you can use the spread operator. e.g. color().rgbToHex(…rgb)
1 2 3 4 5 6 |
const hex = "#137333" const rgb = colory().hexToRgb(hex) console.log("From HEX", hex ,"To RGB",rgb) // RESULT: // HEX #137333 To RGB [ 19, 115, 51 ] |
Parameters
- {Number}
r
– red 3-digit colour code. - {Number}
g
– green 3-digit colour code. - {Number}
b
– blue 3-digit colour code.
Returns
String – Hexadecimal string.
hexToRgb(hex)
Converts Hexadecimal colour to RGB
1 2 3 4 5 6 |
const rgb = [ 19, 115, 51 ] const hex = colory().rgbToHex(...rgb) console.log("from RGB", rgb, "To HEX", hex) // Result: // from RGB [ 19, 115, 51 ] To HEX #137333 |
Parameters
- {String}
hex
– hexadecimal colour as a string.
Returns
[Number, Number, Number] – RGB number set for the target colour.
generateColorGradient(colorStart, colorEnd, steps)
Generates a colour gradient based on the start and end colours at a set number of steps. Start and end colours can be added as hexadecimal string values or RGB number values within an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// Example 1: start and end colour as RGB const gradient = colory().generateColorGradient([0, 0, 255], [255, 0, 0], 20) console.log(gradient) // Result: // [ // [ 0, 0, 255 ], [ 13, 0, 242 ], // [ 27, 0, 228 ], [ 40, 0, 215 ], // [ 54, 0, 201 ], [ 67, 0, 188 ], // [ 81, 0, 174 ], [ 94, 0, 161 ], // [ 107, 0, 148 ], [ 121, 0, 134 ], // [ 134, 0, 121 ], [ 148, 0, 107 ], // [ 161, 0, 94 ], [ 174, 0, 81 ], // [ 188, 0, 67 ], [ 201, 0, 54 ], // [ 215, 0, 40 ], [ 228, 0, 27 ], // [ 242, 0, 13 ], [ 255, 0, 0 ] // ] // Example 1: start and end colour as HEX const gradient = colory().generateColorGradient("#0000ff" , "#ff0100", 20) console.log(gradient) // Result: // [ // [ 0, 0, 255 ], [ 13, 0, 242 ], // [ 27, 0, 228 ], [ 40, 0, 215 ], // [ 54, 0, 201 ], [ 67, 0, 188 ], // [ 81, 0, 174 ], [ 94, 0, 161 ], // [ 107, 0, 148 ], [ 121, 0, 134 ], // [ 134, 0, 121 ], [ 148, 0, 107 ], // [ 161, 0, 94 ], [ 174, 0, 81 ], // [ 188, 0, 67 ], [ 201, 0, 54 ], // [ 215, 0, 40 ], [ 228, 0, 27 ], // [ 242, 0, 13 ], [ 255, 0, 0 ] // ] |
Parameters
- {String|[Number, Number, Number]}
colorStart
– Start colour as Hexadecimal string or set of 3 RGB 3-digit number series. - {String|[Number, Number, Number]}
colorEnd
– Start colour as Hexadecimal string or set of 3 RGB 3-digit number series. - {Number}
steps
– total number of steps to break down into.
Returns
{[[Number, Number, Number], …]} – A 2d array of RGB number arrays at the set step length.
Conclusion
This is mostly used for solo projects, often in Google Apps Script. As such, I have not included any error handling. This also increased performance because the code has less work to do. The JS doc tooltips are enough for me here and you could deduce most errors from natural errors caused in JavaScript.
My recommendation is to only add the methods that you believe you are going to use and remove the rest. No need to bloat out your own project.
Having said that, let me know if there are some other methods you would like to see in the class and I will have a crack at adding them in.
~ Yagi