Skip to content

Commit

Permalink
feat: Add convertRGBtoHex (#17)
Browse files Browse the repository at this point in the history
* feat: Add convertRGBtoHex

* chore: Add changeset log
  • Loading branch information
Marabyte authored Aug 27, 2022
1 parent 6334b4c commit 97eb272
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-numbers-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sardine/colour": minor
---

Add convertRGBtoHex
18 changes: 16 additions & 2 deletions src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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}`);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { ciede2000 } from "./CIEDE2000.js";
export {
convertHextoRGB,
convertRGBtoHex,
convertRGBtoLab,
convertRGBtoXYZ,
convertXYZtoLab,
Expand Down
30 changes: 28 additions & 2 deletions src/tests/converters.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
});

0 comments on commit 97eb272

Please sign in to comment.