diff --git a/.changeset/eleven-numbers-peel.md b/.changeset/eleven-numbers-peel.md new file mode 100644 index 0000000..c0ef3be --- /dev/null +++ b/.changeset/eleven-numbers-peel.md @@ -0,0 +1,5 @@ +--- +"@sardine/colour": minor +--- + +Add convertRGBtoHex diff --git a/src/converters.ts b/src/converters.ts index 8ca668f..04b5081 100644 --- a/src/converters.ts +++ b/src/converters.ts @@ -50,8 +50,7 @@ export function convertXYZtoLab(colour: XYZColour): LabColour { /** * Indirectly converts RGB to Lab. - * First converts RGB to XYZ, - * Then converts XYZ to Lab + * First converts RGB to XYZ, then converts XYZ to Lab. * @param {RGBColour} colour sRGB colour * @return {LabColour} Lab colour */ @@ -60,6 +59,21 @@ export function convertRGBtoLab(colour: RGBColour): LabColour { return convertXYZtoLab(XYZColour); } +/** + * Converts a colour in the RGB format to Hexadecimal. + * It accepts an option Alpha channel `A` + * @param {Object} colour - An object representing RGB Colour. + * @param {number} colour.R - A number between 0 and 255 to describe the Red colour channel + * @param {number} colour.G - A number between 0 and 255 to describe the Green colour channel + * @param {number} colour.B - A number between 0 and 255 to describe the Blue colour channel + * @param {number} colour.A - An optional number between 0 and 1 to describe the Alpha colour channel + * @returns - An hexadecimal string + */ +export function convertRGBtoHex({R, G, B, A}: RGBColour): string { + const hex = (n: number) => n.toString(16).padStart(2, '0'); + return '#'+hex(R)+hex(G)+hex(B)+(A ? hex(Math.round(A * 255)) : ''); +} + export function convertHextoRGB(hex: string): RGBColour { if (typeof hex !== "string") { throw new Error(`convertHextoRGB expects a string but got a ${typeof hex}`); diff --git a/src/index.ts b/src/index.ts index 9439c7e..7611387 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ export { ciede2000 } from "./CIEDE2000.js"; export { convertHextoRGB, + convertRGBtoHex, convertRGBtoLab, convertRGBtoXYZ, convertXYZtoLab, diff --git a/src/tests/converters.test.ts b/src/tests/converters.test.ts index ab0d2be..98f2fc0 100644 --- a/src/tests/converters.test.ts +++ b/src/tests/converters.test.ts @@ -1,9 +1,10 @@ import test from "ava"; import { + convertHextoRGB, + convertRGBtoHex, + convertRGBtoLab, convertRGBtoXYZ, convertXYZtoLab, - convertRGBtoLab, - convertHextoRGB, } from "../converters.js"; import { ciede2000 } from "../CIEDE2000.js"; import type { LabColour, RGBColour, XYZColour } from "../types"; @@ -153,3 +154,28 @@ test("throws an error if not passing a valid hexadecimal value", ({ "convertHextoRGB expects an valid hexadecimal colour value but got HH77ZZs" ); }); + +test("converts RGB format to hexadecimal colour", ({ + is +}) => { + const expectedHex = "#ffffff"; + const RGB: RGBColour = { + R: 255, + G: 255, + B: 255, + }; + is(convertRGBtoHex(RGB), expectedHex); +}); + +test("converts RGBA format to hexadecimal colour with alpha channel", ({ + is +}) => { + const expectedHex = "#ffffff80"; + const RGB: RGBColour = { + R: 255, + G: 255, + B: 255, + A: 0.5 + }; + is(convertRGBtoHex(RGB), expectedHex); +}); \ No newline at end of file