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

colors-convert

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

colors-convert - npm Package Compare versions

Comparing version 1.0.3 to 1.0.6

dist/index.js.map

3

dist/index.d.ts
export { isHex, isRgb, isRgba, isCmyk, isColor } from './types/types';
export { color2string, color2cssString, hex2rgbOrRgba, hex2rgba, rgb2hex, hex2hexWithAlpha, } from './lib/color-utils';
export { color2string, color2cssString, hex2rgbOrRgba, hex2rgba, rgb2hex, hex2hexWithAlpha, hex2cmyk } from './lib/color-utils';
export { getRandomColor } from './lib/random';

@@ -16,1 +16,5 @@ "use strict";

exports.hex2hexWithAlpha = color_utils_1.hex2hexWithAlpha;
exports.hex2cmyk = color_utils_1.hex2cmyk;
var random_1 = require("./lib/random");
exports.getRandomColor = random_1.getRandomColor;
//# sourceMappingURL=index.js.map

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

import { COLOR, RGB, RGBA } from '../types/types';
import { COLOR, RGB, RGBA, CMYK } from '../types/types';
export declare const color2string: (color: COLOR) => string;

@@ -8,1 +8,2 @@ export declare const color2cssString: (color: COLOR) => string;

export declare const hex2hexWithAlpha: (hex: string, alpha: number) => string;
export declare const hex2cmyk: (hex: string) => CMYK;

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

// TODO: add check that color is a valid color
// Convert a color to a string format
exports.color2string = function (color) {

@@ -36,2 +37,3 @@ if (types_1.isHex(color)) {

// TODO: add check that color is a valid color
// Convert a color to a string format usable in CSS
exports.color2cssString = function (color) {

@@ -54,2 +56,3 @@ if (types_1.isHex(color)) {

// TODO: add check that hex is a valid hex
// Convert an hex to a rgb or rgba color (depeds on hex format)
exports.hex2rgbOrRgba = function (hex) {

@@ -77,2 +80,3 @@ var RGB_HEX = /^#?(?:([0-9a-f]{3})|([0-9a-f]{6})([0-9a-f]{2})?)$/i;

// TODO: add check that hex is a valid hex
// Convert an hex to a rgba object
exports.hex2rgba = function (hex, alpha) {

@@ -95,2 +99,3 @@ if (alpha === void 0) { alpha = 1; }

// TODO: add check that rgb is a valid rgb
// Convert an rgb object to hex
exports.rgb2hex = function (rgb) {

@@ -107,2 +112,3 @@ var r = rgb.r, g = rgb.g, b = rgb.b;

};
// Convert an hex to another hex with the given alpha
exports.hex2hexWithAlpha = function (hex, alpha) {

@@ -117,1 +123,25 @@ if (!utils_1.between(alpha, [0, 1])) {

};
// TODO: consider also alpha
// TODO: check all types of hex formats
// Convert an hex to a cmyk
exports.hex2cmyk = function (hex) {
var _a = exports.hex2rgba(hex), r = _a.r, g = _a.g, b = _a.b;
var c = 0;
var m = 0;
var y = 0;
var k = 0;
if (r === 0 && g === 0 && b === 0) {
k = 1;
return { c: c, m: m, y: y, k: k };
}
c = 1 - r / 255;
m = 1 - g / 255;
y = 1 - b / 255;
var minCMY = Math.min(c, Math.min(m, y));
c = (c - minCMY) / (1 - minCMY);
m = (m - minCMY) / (1 - minCMY);
y = (y - minCMY) / (1 - minCMY);
k = minCMY;
return { c: c, m: m, y: y, k: k };
};
//# sourceMappingURL=color-utils.js.map

@@ -9,1 +9,2 @@ "use strict";

exports.sameContent = function (a, b) { return a.sort().toString() == b.sort().toString(); };
//# sourceMappingURL=utils.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../index");
var colors_1 = require("./colors");
////////////////////////////////////////////////////////
// color2string
////////////////////////////////////////////////////////
var STRING_VALID = [
'#ffffff',
{ r: 0, g: 0, b: 0 },
{ r: 0, g: 0, b: 0, a: 0 },
{ c: 0, m: 0, y: 0, k: 0 },
];
var STRING_VALID_CHECK = ['#FFFFFF', "0, 0, 0", "0, 0, 0, 0", "0%, 0%, 0%, 0%"];
STRING_VALID.forEach(function (color, i) {
return test(JSON.stringify(color), function () { return expect(index_1.color2string(color)).toBe(STRING_VALID_CHECK[i]); });
test("color2string", function () {
expect(index_1.color2string('#000000')).toBe('#000000');
expect(index_1.color2string({ r: 0, g: 0, b: 0 })).toBe('0, 0, 0');
expect(index_1.color2string({ r: 0, g: 0, b: 0, a: 0 })).toBe('0, 0, 0, 0');
expect(index_1.color2string({ c: 0, m: 0, y: 0, k: 0 })).toBe('0%, 0%, 0%, 0%');
});

@@ -21,16 +16,7 @@ ////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
var STRING_CSS_VALID = [
'#ffffff',
{ r: 0, g: 0, b: 0 },
{ r: 0, g: 0, b: 0, a: 0 },
{ c: 0, m: 0, y: 0, k: 0 },
];
var STRING_CSS_VALID_CHECK = [
'#FFFFFF',
"rgb(0, 0, 0)",
"rgba(0, 0, 0, 0)",
"cmyk(0%, 0%, 0%, 0%)",
];
STRING_CSS_VALID.forEach(function (color, i) {
return test(JSON.stringify(color), function () { return expect(index_1.color2cssString(color)).toBe(STRING_CSS_VALID_CHECK[i]); });
test("color2cssString", function () {
expect(index_1.color2cssString('#000000')).toBe('#000000');
expect(index_1.color2cssString({ r: 0, g: 0, b: 0 })).toBe('rgb(0, 0, 0)');
expect(index_1.color2cssString({ r: 0, g: 0, b: 0, a: 0 })).toBe('rgba(0, 0, 0, 0)');
expect(index_1.color2cssString({ c: 0, m: 0, y: 0, k: 0 })).toBe('cmyk(0%, 0%, 0%, 0%)');
});

@@ -40,10 +26,5 @@ ////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
colors_1.colors.forEach(function (_a) {
var name = _a.name, hex = _a.hex, rgb = _a.rgb, rgba = _a.rgba;
return test("hex2rgbOrRgba: " + name + " (" + hex + ")", function () {
if (hex.length > 7)
expect(index_1.hex2rgbOrRgba(hex)).toStrictEqual(rgba);
else
expect(index_1.hex2rgbOrRgba(hex)).toStrictEqual(rgb);
});
test("hex2rgbOrRgba", function () {
expect(index_1.hex2rgbOrRgba('#000000')).toStrictEqual({ r: 0, g: 0, b: 0 });
expect(index_1.hex2rgbOrRgba('#00000000')).toStrictEqual({ r: 0, g: 0, b: 0, a: 0 });
});

@@ -53,5 +34,9 @@ ////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
colors_1.colors.forEach(function (_a) {
var name = _a.name, hex = _a.hex, rgba = _a.rgba, opacity = _a.opacity;
return test("hex2rgba: " + name + " (" + hex + ", " + opacity + ")", function () { return expect(index_1.hex2rgba(hex)).toStrictEqual(rgba); });
test("hex2rgba", function () {
expect(index_1.hex2rgba('#000000')).toStrictEqual({ r: 0, g: 0, b: 0, a: 1 });
expect(index_1.hex2rgba('#00000000')).toStrictEqual({ r: 0, g: 0, b: 0, a: 0 });
expect(index_1.hex2rgba('#000000', 0)).toStrictEqual({ r: 0, g: 0, b: 0, a: 0 });
expect(index_1.hex2rgba('#000000', 1)).toStrictEqual({ r: 0, g: 0, b: 0, a: 1 });
expect(index_1.hex2rgba('#000000', 0.5)).toStrictEqual({ r: 0, g: 0, b: 0, a: 0.5 });
// expect(hex2rgba('#000000', 3)).toThrow(new Error('3 is not in the range [0, 1].'))
});

@@ -61,7 +46,5 @@ ////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
colors_1.colors
.filter(function (c) { return c.hex.length === 7; })
.forEach(function (_a) {
var name = _a.name, hex = _a.hex, rgb = _a.rgb;
return test("rgb2hex: " + name + " (" + hex + ")", function () { return expect(index_1.rgb2hex(rgb)).toStrictEqual(hex); });
test("rgb2hex", function () {
expect(index_1.rgb2hex({ r: 0, g: 0, b: 0 })).toBe('#000000');
expect(index_1.rgb2hex({ r: 255, g: 255, b: 255 })).toBe('#ffffff');
});

@@ -71,10 +54,14 @@ ////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
colors_1.colors
.filter(function (c) { return c.hex.length === 9; })
.forEach(function (_a) {
var name = _a.name, hex = _a.hex, opacity = _a.opacity;
var hexWithoutAlpha = hex.slice(0, 7);
return test("hex2hexWithAlpha: " + name + " (" + hexWithoutAlpha + ")", function () {
return expect(index_1.hex2hexWithAlpha(hexWithoutAlpha, opacity)).toStrictEqual(hex);
});
test("hex2hexWithAlpha", function () {
expect(index_1.hex2hexWithAlpha('#000000', 0)).toBe('#00000000');
expect(index_1.hex2hexWithAlpha('#000000', 1)).toBe('#000000ff');
expect(index_1.hex2hexWithAlpha('#000', 1)).toBe('#000ff');
// expect(hex2hexWithAlpha('#000000', 3)).toThrow(new Error('3 is not in the range [0, 1].'))
});
////////////////////////////////////////////////////////
// hex2cmyk
////////////////////////////////////////////////////////
test("hex2cmyk", function () {
expect(index_1.hex2cmyk('#000000')).toStrictEqual({ c: 0, m: 0, y: 0, k: 1 });
});
//# sourceMappingURL=conversion.test.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../index");
var colors_1 = require("./colors");
////////////////////////////////////////////////////////
// isHex
////////////////////////////////////////////////////////
// test valid hex
colors_1.colors.forEach(function (_a) {
var name = _a.name, hex = _a.hex, opacity = _a.opacity;
return test("isHex: " + name + " (" + hex + ", " + opacity + ")", function () { return expect(index_1.isHex(hex)).toBe(true); });
test("isHex", function () {
expect(index_1.isHex('#000000')).toBe(true);
expect(index_1.isHex('#a00000')).toBe(true);
expect(index_1.isHex('#A00000')).toBe(true);
expect(index_1.isHex('#000')).toBe(true);
expect(index_1.isHex('#00000')).toBe(true);
expect(index_1.isHex('#00000000')).toBe(true);
expect(index_1.isHex('')).toBe(false);
expect(index_1.isHex('#')).toBe(false);
expect(index_1.isHex('#0')).toBe(false);
expect(index_1.isHex('#00')).toBe(false);
expect(index_1.isHex('000')).toBe(false);
expect(index_1.isHex('#0000000')).toBe(false);
expect(index_1.isHex('#000000000')).toBe(false);
expect(index_1.isHex('#00000Z')).toBe(false);
});
// test not valid hex
var HEX_NOT_VALID = ['', '#', '#0', '#00', '#0000', '0a0A00', '#000ZGF', '#FFFFFFGG'];
HEX_NOT_VALID.forEach(function (color) { return test(color, function () { return expect(index_1.isHex(color)).toBe(false); }); });
////////////////////////////////////////////////////////
// isRgb
////////////////////////////////////////////////////////
// test valid rgb
colors_1.colors.forEach(function (_a) {
var name = _a.name, rgb = _a.rgb;
return test("isHex: " + name + " (" + rgb + ")", function () { return expect(index_1.isRgb(rgb)).toBe(true); });
test("isRgb", function () {
expect(index_1.isRgb({ r: 0, g: 0, b: 0 })).toBe(true);
expect(index_1.isRgb({ r: 255, g: 255, b: 255 })).toBe(true);
expect(index_1.isRgb({})).toBe(false);
expect(index_1.isRgb({ r: 0 })).toBe(false);
expect(index_1.isRgb({ b: 0 })).toBe(false);
expect(index_1.isRgb({ r: 0, g: 0, b: 0, a: 1 })).toBe(false);
expect(index_1.isRgb({ r: -1, g: 0, b: 0 })).toBe(false);
expect(index_1.isRgb({ r: 300, g: 0, b: 0 })).toBe(false);
expect(index_1.isRgb({ r: 'zero', g: 0, b: 0 })).toBe(false);
});
// test not valid rgb
var RGB_NOT_VALID = [
{},
{ r: 0 },
{ b: 0 },
{ r: 0, g: 0, b: 0, a: 1 },
{ r: -1, g: 0, b: 0 },
{ r: 300, g: 0, b: 0 },
{ r: 'twenty', g: 0, b: 0 },
];
RGB_NOT_VALID.forEach(function (color) { return test(JSON.stringify(color), function () { return expect(index_1.isRgb(color)).toBe(false); }); });
////////////////////////////////////////////////////////
// isRgba
////////////////////////////////////////////////////////
// test valid rgba
colors_1.colors.forEach(function (_a) {
var name = _a.name, rgba = _a.rgba;
return test("isHex: " + name + " (" + rgba + ")", function () { return expect(index_1.isRgba(rgba)).toBe(true); });
test("isRgba", function () {
expect(index_1.isRgba({ r: 0, g: 0, b: 0, a: 0 })).toBe(true);
expect(index_1.isRgba({ r: 255, g: 255, b: 255, a: 1 })).toBe(true);
expect(index_1.isRgba({})).toBe(false);
expect(index_1.isRgba({ r: 0 })).toBe(false);
expect(index_1.isRgba({ b: 0 })).toBe(false);
expect(index_1.isRgba({ r: 0, g: 0, b: 0, a: 0, o: 3 })).toBe(false);
expect(index_1.isRgba({ r: -1, g: 0, b: 0, a: 0 })).toBe(false);
expect(index_1.isRgba({ r: 300, g: 0, b: 0, a: 0 })).toBe(false);
expect(index_1.isRgba({ r: 'zero', g: 0, b: 0, a: 0 })).toBe(false);
expect(index_1.isRgba({ r: 0, g: 0, b: 0, a: 3 })).toBe(false);
});
// test not valid rgba
var RGBA_NOT_VALID = [
{},
{ r: 0 },
{ b: 0 },
{ r: 0, g: 0, b: 0, a: 0, o: 3 },
{ r: -1, g: 0, b: 0, a: 0 },
{ r: 300, g: 0, b: 0, a: 0 },
{ r: 'twenty', g: 0, b: 0, a: 0 },
];
RGBA_NOT_VALID.forEach(function (color) {
return test(JSON.stringify(color), function () { return expect(index_1.isRgba(color)).toBe(false); });
});
////////////////////////////////////////////////////////
// isCmyk
////////////////////////////////////////////////////////
// test valid cmyk
colors_1.colors.forEach(function (_a) {
var name = _a.name, cmyk = _a.cmyk;
return test("isHex: " + name + " (" + cmyk + ")", function () { return expect(index_1.isCmyk(cmyk)).toBe(true); });
test("isCmyk", function () {
expect(index_1.isCmyk({ c: 0, m: 0, y: 0, k: 0 })).toBe(true);
expect(index_1.isCmyk({ c: 100, m: 100, y: 100, k: 100 })).toBe(true);
expect(index_1.isCmyk({})).toBe(false);
expect(index_1.isCmyk({ c: 0 })).toBe(false);
expect(index_1.isCmyk({ y: 0 })).toBe(false);
expect(index_1.isCmyk({ c: 0, m: 0, y: 0, k: 0, o: 3 })).toBe(false);
expect(index_1.isCmyk({ c: -1, m: 0, y: 0, k: 0 })).toBe(false);
expect(index_1.isCmyk({ c: 300, m: 0, y: 0, k: 0 })).toBe(false);
expect(index_1.isCmyk({ c: 'twenty', m: 0, y: 0, k: 0 })).toBe(false);
});
// test not valid cmyk
var CMYK_NOT_VALID = [
{},
{ c: 0 },
{ y: 0 },
{ c: 0, m: 0, y: 0, k: 0, o: 3 },
{ c: -1, m: 0, y: 0, k: 0 },
{ c: 300, m: 0, y: 0, k: 0 },
{ c: 'twenty', m: 0, y: 0, k: 0 },
];
CMYK_NOT_VALID.forEach(function (color) {
return test(JSON.stringify(color), function () { return expect(index_1.isCmyk(color)).toBe(false); });
});
////////////////////////////////////////////////////////
// isColor
////////////////////////////////////////////////////////
// TODO: add tests
test("isColor", function () {
expect(index_1.isColor('#000000')).toBe(true);
expect(index_1.isColor({ a: '' })).toBe(false);
});
//# sourceMappingURL=types.test.js.map

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

exports.isHex = isHex;
// Accept an object like this {r, g, b} with r,b,g numeric values in range [0, 255]
function isRgb(color) {

@@ -29,2 +30,3 @@ var keys = Object.keys(color);

// TODO: accept also rgba without a, consider it 1 as default
// Accept an object like this {r, g, b, a} with r,g,b numeric values in range [0, 255] and a in range [0,1]
function isRgba(color) {

@@ -44,2 +46,4 @@ var keys = Object.keys(color);

exports.isRgba = isRgba;
// TODO: add support for values in [0, 1]
// Accept an object like this {c, m, y, k} with c,m,y,k numeric values in range [0, 100]
function isCmyk(color) {

@@ -63,1 +67,2 @@ var keys = Object.keys(color);

exports.isColor = isColor;
//# sourceMappingURL=types.js.map
{
"name": "colors-convert",
"version": "1.0.3",
"version": "1.0.6",
"main": "dist/index.js",

@@ -25,4 +25,7 @@ "types": "dist/index.d.ts",

"scripts": {
"build": "tsc",
"compile": "tsc",
"compile-watch": "tsc --watch",
"test": "jest --config jestconfig.json",
"test-watch": "jest --watch --config jestconfig.json",
"prepublish": "rm -rf dist/ && tsc",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",

@@ -37,4 +40,5 @@ "lint": "tslint -p tsconfig.json"

"@types/lodash": "^4.14.144",
"@types/source-map": "^0.5.7",
"lodash": "^4.17.15"
}
}
# Colors-convert
To publish:
Commands:
- `yarn build` to create the dist folder
- `yarn compile` to compile Typescript
- `yarn compile-watch` to compile Typescript in watch mode
- `yarn test` to run Jest tests
- `yarn test-watch` to run Jest tests in watch mode
- `yarn prepublish` to remove the dist folder, ricreate it and compile Typescript
- `yarn publish` to publish the package on NPM
- `yarn format` to format the code using Prettier
- `yarn lint` to lint the code using tslint.
To run test:
So, to publish on NPM:
- `yarn test`
- `yarn compile`
- `yarn prepublish`
- `yarn publish`
- then push with push all tags flag.
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