Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lephenix47/color-converter

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lephenix47/color-converter - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

dist/lib/commonjs/variables/color-names.variables.d.ts

43

dist/lib/commonjs/classes/color-conversion.classes.d.ts

@@ -1,2 +0,2 @@

import { RedGreenBlue, HueSaturationLightness, HueWhitenessBlackness, HueSaturationValue } from "../variables/color-types.variables";
import { RedGreenBlue, HueSaturationLightness, HueWhitenessBlackness, HueSaturationValue, CyanMagentaYellowKey, ColorRepresentation } from "../variables/color-types.variables";
/**

@@ -54,2 +54,26 @@ * Abstract class containing conversion methods for various color models.

fromHsvToRgb(color: HueSaturationValue): RedGreenBlue;
/**
* Converts an RGB color value to CMYK color representation.
* @param {RedGreenBlue} color - The RGB color object.
* @returns {CyanMagentaYellowKey} The CMYK color object.
*/
fromRgbToCmyk(color: RedGreenBlue): CyanMagentaYellowKey;
/**
* Converts a CMYK color value to RGB color representation.
* @param {CyanMagentaYellowKey} color - The CMYK color object.
* @returns {RedGreenBlue} The RGB color object.
*/
fromCmykToRgb(color: CyanMagentaYellowKey): RedGreenBlue;
/**
* Converts a hexadecimal color value to the corresponding color name.
* @param {string} color - The hexadecimal color value.
* @returns {string | null} The corresponding color name, or null if not found.
*/
fromHexToName(color: string): string | null;
/**
* Converts a color name to the corresponding hexadecimal color value.
* @param {string} color - The color name.
* @returns {string | null} The corresponding hexadecimal color value, or null if not found.
*/
fromNameToHex(color: string): string | null;
}

@@ -60,5 +84,5 @@ /**

export declare class ColorConverter extends AbstractConversionMethods {
color: string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue;
color: ColorRepresentation;
private normalizedColor;
currentModel: string;
private normalizedColor;
/**

@@ -69,3 +93,3 @@ * Constructs a ColorConverter object.

*/
constructor(currentModel: string, color: string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue);
constructor(currentModel: string, color: ColorRepresentation);
/**

@@ -75,4 +99,9 @@ * Normalizes the color to RGB format to be later convert back into another one

*/
normalizeToRgb(): RedGreenBlue | void;
private normalizeToRgb;
/**
* Sets a new color + target model to the instance class
* @returns {void}
*/
setNewColor(newColor: ColorRepresentation, newTargetModel: string): void;
/**
* Converts the color to the specified color model.

@@ -82,3 +111,3 @@ * @param {string} targetModel - The target color model.

*/
convertTo(targetModel: string): string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue;
convertTo(targetModel: string): ColorRepresentation;
/**

@@ -88,3 +117,3 @@ * Retrieves all color models for the current color.

*/
getAllColorModels(): (string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue)[];
getAllColorModels(): ColorRepresentation[];
}

@@ -19,2 +19,3 @@ "use strict";

exports.ColorConverter = exports.AbstractConversionMethods = void 0;
var color_names_variables_1 = require("../variables/color-names.variables");
/**

@@ -32,7 +33,29 @@ * Abstract class containing conversion methods for various color models.

AbstractConversionMethods.prototype.fromRgbToHex = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var hexadecimalRed = red.toString(16);
var hexadecimalGreen = green.toString(16);
var hexadecimalBlue = blue.toString(16);
return "#".concat(hexadecimalRed).concat(hexadecimalGreen).concat(hexadecimalBlue, ";");
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var hexadecimalRed = toBase16(red).length < 2 ? "0".concat(toBase16(red)) : toBase16(red);
var hexadecimalGreen = toBase16(green).length < 2 ? "0".concat(toBase16(green)) : toBase16(green);
var hexadecimalBlue = toBase16(blue).length < 2 ? "0".concat(toBase16(blue)) : toBase16(blue);
function toBase16(number) {
return number.toString(16);
}
return "#".concat(hexadecimalRed).concat(hexadecimalGreen).concat(hexadecimalBlue);
};

@@ -45,11 +68,7 @@ /**

AbstractConversionMethods.prototype.fromHexToRgb = function (color) {
var colorArgumentIsInvalid = (color === null || color === void 0 ? void 0 : color.length) < 6 || (color === null || color === void 0 ? void 0 : color.length) > 7;
var colorArgumentIsInvalid = typeof color !== "string" || (color === null || color === void 0 ? void 0 : color.length) < 6 || (color === null || color === void 0 ? void 0 : color.length) > 7;
if (colorArgumentIsInvalid) {
throw new Error("Error: Unexpected color argument length passed, was expecting a 6 or 7 characters long string but instead got ".concat(color.length));
}
var hexColor = color;
var hasHashTag = color.charAt(0) === "#";
if (hasHashTag) {
hexColor = color.slice(1);
}
var hexColor = color.charAt(0) === "#" ? color.slice(1) : color;
var redBase16 = hexColor.substring(0, 2);

@@ -59,9 +78,9 @@ var greeBase16 = hexColor.substring(2, 4);

var base16NumbersArray = [redBase16, greeBase16, blueBase16];
var base10NumbersArrays = [];
for (var i = 0; i < base16NumbersArray.length; i++) {
var colorBase16 = base16NumbersArray[i];
base16NumbersArray[i] = Number("0x".concat(colorBase16));
var colorBase10 = Number("0x".concat(colorBase16));
base10NumbersArrays.push(colorBase10);
}
var redBase10 = Number(base16NumbersArray[0]);
var greenBase10 = Number(base16NumbersArray[1]);
var blueBase10 = Number(base16NumbersArray[2]);
var redBase10 = base10NumbersArrays[0], greenBase10 = base10NumbersArrays[1], blueBase10 = base10NumbersArrays[2];
return { red: redBase10, green: greenBase10, blue: blueBase10 };

@@ -75,2 +94,9 @@ };

AbstractConversionMethods.prototype.fromRgbToHsl = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;

@@ -130,3 +156,3 @@ var argumentsAreInvalid = !Number.isInteger(red) ||

// Round the values and multiply saturation and lightness by 100
var roundedHue = Math.round(hue * 360);
var roundedHue = Math.round(hue * 360) % 360;
var roundedSaturation = Math.round(saturation * 100);

@@ -136,3 +162,3 @@ var roundedLightness = Math.round(lightness * 100);

return {
hue: roundedHue % 360,
hue: roundedHue,
saturation: roundedSaturation,

@@ -148,7 +174,23 @@ lightness: roundedLightness,

AbstractConversionMethods.prototype.fromHslToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("saturation") ||
!color.hasOwnProperty("lightness");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, saturation or lightness");
}
var hue = color.hue, saturation = color.saturation, lightness = color.lightness;
var normalizedSaturation = saturation / 100;
var normalizedLightness = lightness / 100;
/**
* Calculates the color component based on the given color value.
* The color component represents the intensity of a specific color (red, green, or blue)
* in the RGB color model.
*
* @link https://en.wikipedia.org/wiki/Color_space
*
* @param {number} colorValue - The color value to calculate the component for.
* @returns {number} - The calculated color component.
*/
function calculateComponent(colorValue) {
// log({ normalizedSaturation, normalizedLightness });
var colorComponent = (colorValue + hue / 30) % 12;

@@ -173,3 +215,22 @@ var chroma = normalizedSaturation *

AbstractConversionMethods.prototype.fromRgbToHwb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var normalizedRed = red / 255;

@@ -193,2 +254,9 @@ var normalizedGreen = green / 255;

AbstractConversionMethods.prototype.fromHwbToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("whiteness") ||
!color.hasOwnProperty("blackness");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, whiteness or blackness");
}
var hue = color.hue, whiteness = color.whiteness, blackness = color.blackness;

@@ -232,3 +300,22 @@ var normalizedWhiteness = whiteness / 100;

AbstractConversionMethods.prototype.fromRgbToHsv = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var min = Math.min(red, green, blue);

@@ -251,2 +338,9 @@ var max = Math.max(red, green, blue);

AbstractConversionMethods.prototype.fromHsvToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("saturation") ||
!color.hasOwnProperty("value");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, saturation or value");
}
var hue = color.hue, saturation = color.saturation, value = color.value;

@@ -306,2 +400,102 @@ // Normalize saturation and value to the range of 0-1

};
/**
* Converts an RGB color value to CMYK color representation.
* @param {RedGreenBlue} color - The RGB color object.
* @returns {CyanMagentaYellowKey} The CMYK color object.
*/
AbstractConversionMethods.prototype.fromRgbToCmyk = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var normalizedRed = red / 255;
var normalizedGreen = green / 255;
var normalizedBlue = blue / 255;
var maxRgbValue = Math.max(normalizedRed, normalizedGreen, normalizedBlue);
var cyan = Math.round((1 - normalizedRed / maxRgbValue) * 100);
var magenta = Math.round((1 - normalizedGreen / maxRgbValue) * 100);
var yellow = Math.round((1 - normalizedBlue / maxRgbValue) * 100);
var key = Math.round((1 - maxRgbValue) * 100);
return { cyan: cyan, magenta: magenta, yellow: yellow, key: key };
};
/**
* Converts a CMYK color value to RGB color representation.
* @param {CyanMagentaYellowKey} color - The CMYK color object.
* @returns {RedGreenBlue} The RGB color object.
*/
AbstractConversionMethods.prototype.fromCmykToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("cyan") ||
!color.hasOwnProperty("magenta") ||
!color.hasOwnProperty("yellow") ||
!color.hasOwnProperty("key");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: cyan, magenta, yellow or key ");
}
var cyan = color.cyan, magenta = color.magenta, yellow = color.yellow, key = color.key;
var normalizedCyan = cyan / 100;
var normalizedMagenta = magenta / 100;
var normalizedYellow = yellow / 100;
var normalizedKey = key / 100;
var maxCmykValue = 1 - normalizedKey;
var red = Math.round((1 - normalizedCyan) * maxCmykValue * 255);
var green = Math.round((1 - normalizedMagenta) * maxCmykValue * 255);
var blue = Math.round((1 - normalizedYellow) * maxCmykValue * 255);
return { red: red, green: green, blue: blue };
};
/**
* Converts a hexadecimal color value to the corresponding color name.
* @param {string} color - The hexadecimal color value.
* @returns {string | null} The corresponding color name, or null if not found.
*/
AbstractConversionMethods.prototype.fromHexToName = function (color) {
var argumentIsInvalid = typeof color !== "string" || color.length < 6 || color.length > 7;
if (argumentIsInvalid) {
throw new Error("Argument passed is invalid: ".concat(typeof color !== "string"
? "not a string"
: "has wrong hex length: ".concat(color.length)));
}
var normalizedColor = color.charAt(0) === "#" ? color.slice(1) : color;
normalizedColor = normalizedColor.toLowerCase();
var nameColorObject = color_names_variables_1.colorArray.find(function (currentNameColorObject) {
var hexValue = currentNameColorObject.hexValue;
var normalizedHexValue = hexValue.toLowerCase();
return normalizedColor === normalizedHexValue;
});
return (nameColorObject === null || nameColorObject === void 0 ? void 0 : nameColorObject.name) || null;
};
/**
* Converts a color name to the corresponding hexadecimal color value.
* @param {string} color - The color name.
* @returns {string | null} The corresponding hexadecimal color value, or null if not found.
*/
AbstractConversionMethods.prototype.fromNameToHex = function (color) {
var argumentIsInvalid = typeof color !== "string";
if (argumentIsInvalid) {
throw new Error("Argument passed is invalid: not a color name string");
}
var normalizedColor = color.toLowerCase();
var nameColorObject = color_names_variables_1.colorArray.find(function (currentNameColorObject) {
var name = currentNameColorObject.name;
var normalizedName = name.toLowerCase();
return normalizedColor === normalizedName;
});
return (nameColorObject === null || nameColorObject === void 0 ? void 0 : nameColorObject.hexValue) || null;
};
return AbstractConversionMethods;

@@ -322,5 +516,4 @@ }());

var _this = _super.call(this) || this;
_this.color = color;
_this.normalizedColor;
_this.currentModel = currentModel;
_this.setNewColor(color, currentModel);
_this.normalizedColor = { red: 0, blue: 0, green: 0 };
_this.normalizeToRgb();

@@ -355,4 +548,13 @@ return _this;

}
case "cmyk": {
this.normalizedColor = this.fromCmykToRgb(this.color);
break;
}
case "name": {
var hexColor = this.fromNameToHex(this.color);
this.normalizedColor = this.fromHexToRgb(hexColor);
break;
}
default: {
throw new Error("Invalid color model.");
throw new Error("Invalid color model for \"".concat(this.currentModel, "\""));
}

@@ -362,2 +564,11 @@ }

/**
* Sets a new color + target model to the instance class
* @returns {void}
*/
ColorConverter.prototype.setNewColor = function (newColor, newTargetModel) {
this.color = newColor;
this.currentModel = newTargetModel.toLowerCase();
this.normalizeToRgb();
};
/**
* Converts the color to the specified color model.

@@ -368,2 +579,3 @@ * @param {string} targetModel - The target color model.

ColorConverter.prototype.convertTo = function (targetModel) {
targetModel = targetModel.toLowerCase();
switch (targetModel) {

@@ -385,4 +597,11 @@ case "hex": {

}
case "cmyk": {
return this.fromRgbToCmyk(this.normalizedColor);
}
case "name": {
var hexColor = this.fromRgbToHex(this.normalizedColor);
return this.fromHexToName(hexColor);
}
default: {
throw new Error("Invalid color model.");
throw new Error("Invalid color model for \"".concat(this.currentModel, "\""));
}

@@ -396,8 +615,17 @@ }

ColorConverter.prototype.getAllColorModels = function () {
var hexColor = this.fromRgbToHex(this.normalizedColor);
var rgbColor = this.normalizedColor;
var hslColor = this.fromRgbToHsl(this.normalizedColor);
var hwbColor = this.fromRgbToHwb(this.normalizedColor);
var hsvColor = this.fromRgbToHsv(this.normalizedColor);
var cmykColor = this.fromRgbToCmyk(this.normalizedColor);
var nameColor = this.fromHexToName(hexColor);
return [
this.fromRgbToHex(this.normalizedColor),
this.normalizedColor,
this.fromRgbToHsl(this.normalizedColor),
this.fromRgbToHwb(this.normalizedColor),
this.fromRgbToHsv(this.normalizedColor),
nameColor,
hexColor,
rgbColor,
hslColor,
hwbColor,
hsvColor,
cmykColor,
];

@@ -404,0 +632,0 @@ };

@@ -33,1 +33,18 @@ /**

};
/**
* Represents a color in the Cyan-Magenta-Yellow-Key (CMYK) color model with value in %
*/
export type CyanMagentaYellowKey = {
cyan: number;
magenta: number;
yellow: number;
key: number;
};
/**
* Represents a color with a name of the color
*/
export type NameColor = {
name: string | null;
hexValue: string | null;
};
export type ColorRepresentation = string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue | CyanMagentaYellowKey | NameColor;

@@ -1,2 +0,2 @@

import { RedGreenBlue, HueSaturationLightness, HueWhitenessBlackness, HueSaturationValue } from "../variables/color-types.variables";
import { RedGreenBlue, HueSaturationLightness, HueWhitenessBlackness, HueSaturationValue, CyanMagentaYellowKey, ColorRepresentation } from "../variables/color-types.variables";
/**

@@ -54,2 +54,26 @@ * Abstract class containing conversion methods for various color models.

fromHsvToRgb(color: HueSaturationValue): RedGreenBlue;
/**
* Converts an RGB color value to CMYK color representation.
* @param {RedGreenBlue} color - The RGB color object.
* @returns {CyanMagentaYellowKey} The CMYK color object.
*/
fromRgbToCmyk(color: RedGreenBlue): CyanMagentaYellowKey;
/**
* Converts a CMYK color value to RGB color representation.
* @param {CyanMagentaYellowKey} color - The CMYK color object.
* @returns {RedGreenBlue} The RGB color object.
*/
fromCmykToRgb(color: CyanMagentaYellowKey): RedGreenBlue;
/**
* Converts a hexadecimal color value to the corresponding color name.
* @param {string} color - The hexadecimal color value.
* @returns {string | null} The corresponding color name, or null if not found.
*/
fromHexToName(color: string): string | null;
/**
* Converts a color name to the corresponding hexadecimal color value.
* @param {string} color - The color name.
* @returns {string | null} The corresponding hexadecimal color value, or null if not found.
*/
fromNameToHex(color: string): string | null;
}

@@ -60,5 +84,5 @@ /**

export declare class ColorConverter extends AbstractConversionMethods {
color: string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue;
color: ColorRepresentation;
private normalizedColor;
currentModel: string;
private normalizedColor;
/**

@@ -69,3 +93,3 @@ * Constructs a ColorConverter object.

*/
constructor(currentModel: string, color: string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue);
constructor(currentModel: string, color: ColorRepresentation);
/**

@@ -75,4 +99,9 @@ * Normalizes the color to RGB format to be later convert back into another one

*/
normalizeToRgb(): RedGreenBlue | void;
private normalizeToRgb;
/**
* Sets a new color + target model to the instance class
* @returns {void}
*/
setNewColor(newColor: ColorRepresentation, newTargetModel: string): void;
/**
* Converts the color to the specified color model.

@@ -82,3 +111,3 @@ * @param {string} targetModel - The target color model.

*/
convertTo(targetModel: string): string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue;
convertTo(targetModel: string): ColorRepresentation;
/**

@@ -88,3 +117,3 @@ * Retrieves all color models for the current color.

*/
getAllColorModels(): (string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue)[];
getAllColorModels(): ColorRepresentation[];
}

@@ -16,2 +16,3 @@ var __extends = (this && this.__extends) || (function () {

})();
import { colorArray } from "../variables/color-names.variables";
/**

@@ -29,7 +30,29 @@ * Abstract class containing conversion methods for various color models.

AbstractConversionMethods.prototype.fromRgbToHex = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var hexadecimalRed = red.toString(16);
var hexadecimalGreen = green.toString(16);
var hexadecimalBlue = blue.toString(16);
return "#".concat(hexadecimalRed).concat(hexadecimalGreen).concat(hexadecimalBlue, ";");
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var hexadecimalRed = toBase16(red).length < 2 ? "0".concat(toBase16(red)) : toBase16(red);
var hexadecimalGreen = toBase16(green).length < 2 ? "0".concat(toBase16(green)) : toBase16(green);
var hexadecimalBlue = toBase16(blue).length < 2 ? "0".concat(toBase16(blue)) : toBase16(blue);
function toBase16(number) {
return number.toString(16);
}
return "#".concat(hexadecimalRed).concat(hexadecimalGreen).concat(hexadecimalBlue);
};

@@ -42,11 +65,7 @@ /**

AbstractConversionMethods.prototype.fromHexToRgb = function (color) {
var colorArgumentIsInvalid = (color === null || color === void 0 ? void 0 : color.length) < 6 || (color === null || color === void 0 ? void 0 : color.length) > 7;
var colorArgumentIsInvalid = typeof color !== "string" || (color === null || color === void 0 ? void 0 : color.length) < 6 || (color === null || color === void 0 ? void 0 : color.length) > 7;
if (colorArgumentIsInvalid) {
throw new Error("Error: Unexpected color argument length passed, was expecting a 6 or 7 characters long string but instead got ".concat(color.length));
}
var hexColor = color;
var hasHashTag = color.charAt(0) === "#";
if (hasHashTag) {
hexColor = color.slice(1);
}
var hexColor = color.charAt(0) === "#" ? color.slice(1) : color;
var redBase16 = hexColor.substring(0, 2);

@@ -56,9 +75,9 @@ var greeBase16 = hexColor.substring(2, 4);

var base16NumbersArray = [redBase16, greeBase16, blueBase16];
var base10NumbersArrays = [];
for (var i = 0; i < base16NumbersArray.length; i++) {
var colorBase16 = base16NumbersArray[i];
base16NumbersArray[i] = Number("0x".concat(colorBase16));
var colorBase10 = Number("0x".concat(colorBase16));
base10NumbersArrays.push(colorBase10);
}
var redBase10 = Number(base16NumbersArray[0]);
var greenBase10 = Number(base16NumbersArray[1]);
var blueBase10 = Number(base16NumbersArray[2]);
var redBase10 = base10NumbersArrays[0], greenBase10 = base10NumbersArrays[1], blueBase10 = base10NumbersArrays[2];
return { red: redBase10, green: greenBase10, blue: blueBase10 };

@@ -72,2 +91,9 @@ };

AbstractConversionMethods.prototype.fromRgbToHsl = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;

@@ -127,3 +153,3 @@ var argumentsAreInvalid = !Number.isInteger(red) ||

// Round the values and multiply saturation and lightness by 100
var roundedHue = Math.round(hue * 360);
var roundedHue = Math.round(hue * 360) % 360;
var roundedSaturation = Math.round(saturation * 100);

@@ -133,3 +159,3 @@ var roundedLightness = Math.round(lightness * 100);

return {
hue: roundedHue % 360,
hue: roundedHue,
saturation: roundedSaturation,

@@ -145,7 +171,23 @@ lightness: roundedLightness,

AbstractConversionMethods.prototype.fromHslToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("saturation") ||
!color.hasOwnProperty("lightness");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, saturation or lightness");
}
var hue = color.hue, saturation = color.saturation, lightness = color.lightness;
var normalizedSaturation = saturation / 100;
var normalizedLightness = lightness / 100;
/**
* Calculates the color component based on the given color value.
* The color component represents the intensity of a specific color (red, green, or blue)
* in the RGB color model.
*
* @link https://en.wikipedia.org/wiki/Color_space
*
* @param {number} colorValue - The color value to calculate the component for.
* @returns {number} - The calculated color component.
*/
function calculateComponent(colorValue) {
// log({ normalizedSaturation, normalizedLightness });
var colorComponent = (colorValue + hue / 30) % 12;

@@ -170,3 +212,22 @@ var chroma = normalizedSaturation *

AbstractConversionMethods.prototype.fromRgbToHwb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var normalizedRed = red / 255;

@@ -190,2 +251,9 @@ var normalizedGreen = green / 255;

AbstractConversionMethods.prototype.fromHwbToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("whiteness") ||
!color.hasOwnProperty("blackness");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, whiteness or blackness");
}
var hue = color.hue, whiteness = color.whiteness, blackness = color.blackness;

@@ -229,3 +297,22 @@ var normalizedWhiteness = whiteness / 100;

AbstractConversionMethods.prototype.fromRgbToHsv = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var min = Math.min(red, green, blue);

@@ -248,2 +335,9 @@ var max = Math.max(red, green, blue);

AbstractConversionMethods.prototype.fromHsvToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("hue") ||
!color.hasOwnProperty("saturation") ||
!color.hasOwnProperty("value");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: hue, saturation or value");
}
var hue = color.hue, saturation = color.saturation, value = color.value;

@@ -303,2 +397,102 @@ // Normalize saturation and value to the range of 0-1

};
/**
* Converts an RGB color value to CMYK color representation.
* @param {RedGreenBlue} color - The RGB color object.
* @returns {CyanMagentaYellowKey} The CMYK color object.
*/
AbstractConversionMethods.prototype.fromRgbToCmyk = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("red") ||
!color.hasOwnProperty("green") ||
!color.hasOwnProperty("blue");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: red, green or blue");
}
var red = color.red, green = color.green, blue = color.blue;
var argumentsAreInvalid = !Number.isInteger(red) ||
!Number.isInteger(green) ||
!Number.isInteger(blue) ||
red < 0 ||
red > 255 ||
green < 0 ||
green > 255 ||
blue < 0 ||
blue > 255;
if (argumentsAreInvalid) {
throw new Error("Invalid RGB color values. Expected integers between 0 and 255, but received: red=".concat(red, ", green=").concat(green, ", blue=").concat(blue));
}
var normalizedRed = red / 255;
var normalizedGreen = green / 255;
var normalizedBlue = blue / 255;
var maxRgbValue = Math.max(normalizedRed, normalizedGreen, normalizedBlue);
var cyan = Math.round((1 - normalizedRed / maxRgbValue) * 100);
var magenta = Math.round((1 - normalizedGreen / maxRgbValue) * 100);
var yellow = Math.round((1 - normalizedBlue / maxRgbValue) * 100);
var key = Math.round((1 - maxRgbValue) * 100);
return { cyan: cyan, magenta: magenta, yellow: yellow, key: key };
};
/**
* Converts a CMYK color value to RGB color representation.
* @param {CyanMagentaYellowKey} color - The CMYK color object.
* @returns {RedGreenBlue} The RGB color object.
*/
AbstractConversionMethods.prototype.fromCmykToRgb = function (color) {
var hasNotNecessaryProperties = !color.hasOwnProperty("cyan") ||
!color.hasOwnProperty("magenta") ||
!color.hasOwnProperty("yellow") ||
!color.hasOwnProperty("key");
// Verify color object properties
if (hasNotNecessaryProperties) {
throw new Error("Invalid color object. Missing required properties: cyan, magenta, yellow or key ");
}
var cyan = color.cyan, magenta = color.magenta, yellow = color.yellow, key = color.key;
var normalizedCyan = cyan / 100;
var normalizedMagenta = magenta / 100;
var normalizedYellow = yellow / 100;
var normalizedKey = key / 100;
var maxCmykValue = 1 - normalizedKey;
var red = Math.round((1 - normalizedCyan) * maxCmykValue * 255);
var green = Math.round((1 - normalizedMagenta) * maxCmykValue * 255);
var blue = Math.round((1 - normalizedYellow) * maxCmykValue * 255);
return { red: red, green: green, blue: blue };
};
/**
* Converts a hexadecimal color value to the corresponding color name.
* @param {string} color - The hexadecimal color value.
* @returns {string | null} The corresponding color name, or null if not found.
*/
AbstractConversionMethods.prototype.fromHexToName = function (color) {
var argumentIsInvalid = typeof color !== "string" || color.length < 6 || color.length > 7;
if (argumentIsInvalid) {
throw new Error("Argument passed is invalid: ".concat(typeof color !== "string"
? "not a string"
: "has wrong hex length: ".concat(color.length)));
}
var normalizedColor = color.charAt(0) === "#" ? color.slice(1) : color;
normalizedColor = normalizedColor.toLowerCase();
var nameColorObject = colorArray.find(function (currentNameColorObject) {
var hexValue = currentNameColorObject.hexValue;
var normalizedHexValue = hexValue.toLowerCase();
return normalizedColor === normalizedHexValue;
});
return (nameColorObject === null || nameColorObject === void 0 ? void 0 : nameColorObject.name) || null;
};
/**
* Converts a color name to the corresponding hexadecimal color value.
* @param {string} color - The color name.
* @returns {string | null} The corresponding hexadecimal color value, or null if not found.
*/
AbstractConversionMethods.prototype.fromNameToHex = function (color) {
var argumentIsInvalid = typeof color !== "string";
if (argumentIsInvalid) {
throw new Error("Argument passed is invalid: not a color name string");
}
var normalizedColor = color.toLowerCase();
var nameColorObject = colorArray.find(function (currentNameColorObject) {
var name = currentNameColorObject.name;
var normalizedName = name.toLowerCase();
return normalizedColor === normalizedName;
});
return (nameColorObject === null || nameColorObject === void 0 ? void 0 : nameColorObject.hexValue) || null;
};
return AbstractConversionMethods;

@@ -319,5 +513,4 @@ }());

var _this = _super.call(this) || this;
_this.color = color;
_this.normalizedColor;
_this.currentModel = currentModel;
_this.setNewColor(color, currentModel);
_this.normalizedColor = { red: 0, blue: 0, green: 0 };
_this.normalizeToRgb();

@@ -352,4 +545,13 @@ return _this;

}
case "cmyk": {
this.normalizedColor = this.fromCmykToRgb(this.color);
break;
}
case "name": {
var hexColor = this.fromNameToHex(this.color);
this.normalizedColor = this.fromHexToRgb(hexColor);
break;
}
default: {
throw new Error("Invalid color model.");
throw new Error("Invalid color model for \"".concat(this.currentModel, "\""));
}

@@ -359,2 +561,11 @@ }

/**
* Sets a new color + target model to the instance class
* @returns {void}
*/
ColorConverter.prototype.setNewColor = function (newColor, newTargetModel) {
this.color = newColor;
this.currentModel = newTargetModel.toLowerCase();
this.normalizeToRgb();
};
/**
* Converts the color to the specified color model.

@@ -365,2 +576,3 @@ * @param {string} targetModel - The target color model.

ColorConverter.prototype.convertTo = function (targetModel) {
targetModel = targetModel.toLowerCase();
switch (targetModel) {

@@ -382,4 +594,11 @@ case "hex": {

}
case "cmyk": {
return this.fromRgbToCmyk(this.normalizedColor);
}
case "name": {
var hexColor = this.fromRgbToHex(this.normalizedColor);
return this.fromHexToName(hexColor);
}
default: {
throw new Error("Invalid color model.");
throw new Error("Invalid color model for \"".concat(this.currentModel, "\""));
}

@@ -393,8 +612,17 @@ }

ColorConverter.prototype.getAllColorModels = function () {
var hexColor = this.fromRgbToHex(this.normalizedColor);
var rgbColor = this.normalizedColor;
var hslColor = this.fromRgbToHsl(this.normalizedColor);
var hwbColor = this.fromRgbToHwb(this.normalizedColor);
var hsvColor = this.fromRgbToHsv(this.normalizedColor);
var cmykColor = this.fromRgbToCmyk(this.normalizedColor);
var nameColor = this.fromHexToName(hexColor);
return [
this.fromRgbToHex(this.normalizedColor),
this.normalizedColor,
this.fromRgbToHsl(this.normalizedColor),
this.fromRgbToHwb(this.normalizedColor),
this.fromRgbToHsv(this.normalizedColor),
nameColor,
hexColor,
rgbColor,
hslColor,
hwbColor,
hsvColor,
cmykColor,
];

@@ -401,0 +629,0 @@ };

@@ -33,1 +33,18 @@ /**

};
/**
* Represents a color in the Cyan-Magenta-Yellow-Key (CMYK) color model with value in %
*/
export type CyanMagentaYellowKey = {
cyan: number;
magenta: number;
yellow: number;
key: number;
};
/**
* Represents a color with a name of the color
*/
export type NameColor = {
name: string | null;
hexValue: string | null;
};
export type ColorRepresentation = string | RedGreenBlue | HueSaturationLightness | HueWhitenessBlackness | HueSaturationValue | CyanMagentaYellowKey | NameColor;

8

package.json
{
"name": "@lephenix47/color-converter",
"version": "1.0.7",
"description": "This is a versatile color conversion library for JavaScript. It provides a convenient way to convert color values between different color models, including RGB, HEX, HSL, HWB, and HSV. With @lephenix47/color-converter, you can effortlessly convert colors, normalize them to a consistent format, and retrieve color values in various color models.",
"version": "1.1.0",
"description": "This is a versatile color conversion library for JavaScript. It provides a convenient way to convert color values between different color models, including color names, RGB, HEX, HSL, HWB, HSV and CMYK. With @lephenix47/color-converter, you can effortlessly convert colors, normalize them to a consistent format, and retrieve color values in various color models.",
"main": "dist/lib/es6/index.js",

@@ -9,4 +9,4 @@ "module": "dist/lib/commonjs/index.js",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"ts": "rm -rf dist/lib && tsc && tsc --build tsconfig.es5.json",
"ts-cmd": "rmdir /s /q dist && tsc && tsc --build tsconfig.es5.json",
"ts-bash": "rm -rf ./dist && tsc && tsc --build tsconfig.es5.json",
"publish": "npm publish --access=public"

@@ -13,0 +13,0 @@ },

@@ -11,3 +11,5 @@ # @lephenix47/color-converter

- [Importing the Library](#importing-the-library)
- [Supported color models](#supported-color-models)
- [Creating Color Objects](#creating-color-objects)
- [Main methods available](#main-methods-available)
- [Converting Color Models](#converting-color-models)

@@ -46,5 +48,10 @@ - [Available Conversion Methods](#available-conversion-methods)

### Supported color models
Currently the library supports 7 colors models:
`color names`, `RGB`, `HEX`, `HSL`, `HWB`, `HSV` and `CMYK`
### Creating Color Objects
To represent colors using the `RGB`, `HSL`, `HWB`, and `HSV` color models, you can use JavaScript objects. Each object should contain the appropriate properties for the corresponding color model. Here's an example:
To represent colors using the different color models, you can use JavaScript objects. Each object should contain the appropriate properties for the corresponding color model. Here's an example:

@@ -56,11 +63,25 @@ ```js

const hsvColor = { hue: 200, saturation: 28, value: 35 };
const cmyk = { cyan:0, magenta: 100, yellow: 100, key: 0 };
```
On the other hand, you can specify the hexadecimal value as a string, with or without the '#' symbol. For example:
On the other hand, you can specify the hexadecimal value as a string, with or without the '#' symbol.
**For example:**
```js
const color = "#0000FF"; // Hexadecimal color with the '#' symbol
const color2 = "AA83F5"; // Hexadecimal color without the '#' symbol
const colorHex1 = "#0000FF"; // Hexadecimal color with the '#' symbol
const colorHex2 = "AA83F5"; // Hexadecimal color without the '#' symbol
const colorName1 = "rebeccapurple" //All lowercase
const colorName2 = "DarkBlue" //In titlecase
```
### Main methods available
There are currently 2 main methods:
- `convertTo(targetModel)`: Method that converts a color into another model, the target model must be either one of the supported 7 color models
- `getAllModels()`: Method that returns an array containing all the color models
### Converting Color Models

@@ -72,5 +93,5 @@

It is important that rgb, hsl, hwb and hsv values are create with an object for example:
It is important that rgb, hsl, hwb, hsv and cmyk values are created with an object
For example:
**For example:**

@@ -82,3 +103,3 @@ ```js

2. Use the `convertTo` method to convert the color to the desired color model. For example, to convert to the hexadecimal representation:
1. Use the `convertTo` method to convert the color to the desired color model. For example, to convert to the hexadecimal representation:

@@ -91,4 +112,6 @@ ```js

To get a list of all available color models supported by `@lephenix47/color-converter`, you can use the `getAllColorModels` method. This method returns an array of strings representing the supported color models. For example:
To get a list of all available color models supported by `@lephenix47/color-converter`, you can use the `getAllColorModels` method. This method returns an array of strings representing the supported color models.
**For example:**
```js

@@ -95,0 +118,0 @@ const allModels = converter.getAllColorModels();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc