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

simpler-color

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpler-color - npm Package Compare versions

Comparing version 0.3.2 to 1.0.0

23

es/color/angle.js

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

"use strict";
exports.__esModule = true;
exports.default = angle;
exports.normalizeAngle = normalizeAngle;
exports.toDegrees = toDegrees;
function toDegrees(angle, unit) {
var _multiplier$unit$toLo;
export function toDegrees(angle, unit) {
const multiplier = {

@@ -16,15 +7,11 @@ rad: 180 / Math.PI,

};
return angle * ((_multiplier$unit$toLo = multiplier[unit.toLowerCase()]) != null ? _multiplier$unit$toLo : 1);
return angle * (multiplier[unit.toLowerCase()] ?? 1);
}
function normalizeAngle(degrees) {
export function normalizeAngle(degrees) {
return (degrees % 360 + 360) % 360;
}
function angle(str) {
var _str$match$, _str$match;
export default function angle(str) {
const num = parseFloat(str);
const unit = (_str$match$ = (_str$match = str.match(/deg|rad|grad|turn/i)) == null ? void 0 : _str$match[0]) != null ? _str$match$ : 'deg';
const unit = str.match(/deg|rad|grad|turn/i)?.[0] ?? 'deg';
return normalizeAngle(toDegrees(num, unit));
}

@@ -1,14 +0,5 @@

"use strict";
import { isHsl } from './hsl';
import { normalizeRgb } from './rgb';
import hslToRgb from './transforms/hslToRgb';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = colorString;
var _hsl = require("./hsl");
var _rgb = require("./rgb");
var _hslToRgb = _interopRequireDefault(require("./transforms/hslToRgb"));
function roundRgb(rgb) {

@@ -35,5 +26,5 @@ return {

function colorString(color) {
const rgbColor = (0, _hsl.isHsl)(color) ? (0, _hslToRgb.default)(color) : (0, _rgb.normalizeRgb)(color);
export default function colorString(color) {
const rgbColor = isHsl(color) ? hslToRgb(color) : normalizeRgb(color);
return rgbColor.a === 1 ? rgbToHexString(rgbColor) : rgbToRgbaString(rgbColor);
}

@@ -1,35 +0,15 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = hsl;
exports.hslFromColorString = hslFromColorString;
exports.isHsl = isHsl;
exports.normalizeHsl = normalizeHsl;
var _utils = require("../utils");
var _angle = _interopRequireWildcard(require("./angle"));
var _hslString = require("./parsers/hslString");
var _rgb = require("./rgb");
var _rgbToHsl = _interopRequireDefault(require("./transforms/rgbToHsl"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function isHsl(color) {
import { clamp } from '../utils';
import angle, { normalizeAngle } from './angle';
import { matchHslString } from './parsers/hslString';
import { rgbFromColorString } from './rgb';
import rgbToHsl from './transforms/rgbToHsl';
export function isHsl(color) {
return typeof color.h === 'number' && typeof color.s === 'number' && typeof color.l === 'number' && typeof color.a === 'number';
}
function normalizeHsl(hsl) {
export function normalizeHsl(hsl) {
return {
h: (0, _angle.normalizeAngle)(hsl.h),
s: (0, _utils.clamp)(hsl.s, 0, 100),
l: (0, _utils.clamp)(hsl.l, 0, 100),
a: (0, _utils.clamp)(hsl.a, 0, 1)
h: normalizeAngle(hsl.h),
s: clamp(hsl.s, 0, 100),
l: clamp(hsl.l, 0, 100),
a: clamp(hsl.a, 0, 1)
};

@@ -39,8 +19,6 @@ }

function hslFromParsedHslString(match) {
var _hslValues$, _match$;
const hslValues = match.map(val => parseFloat(val));
let alpha = (_hslValues$ = hslValues[3]) != null ? _hslValues$ : 1;
let alpha = hslValues[3] ?? 1;
if (((_match$ = match[3]) == null ? void 0 : _match$.indexOf('%')) > -1) {
if (match[3]?.indexOf('%') > -1) {
alpha *= 0.01;

@@ -50,3 +28,3 @@ }

return normalizeHsl({
h: (0, _angle.default)(match[0]),
h: angle(match[0]),
s: hslValues[1],

@@ -59,19 +37,16 @@ l: hslValues[2],

function hslFromRgbString(colorString) {
const rgbColor = (0, _rgb.rgbFromColorString)(colorString);
return rgbColor ? (0, _rgbToHsl.default)(rgbColor) : null;
const rgbColor = rgbFromColorString(colorString);
return rgbColor ? rgbToHsl(rgbColor) : null;
}
function hslFromColorString(colorString) {
export function hslFromColorString(colorString) {
colorString = colorString.trim();
let match;
if ((match = (0, _hslString.matchHslString)(colorString)) !== null) return hslFromParsedHslString(match);
if ((match = matchHslString(colorString)) !== null) return hslFromParsedHslString(match);
return null;
}
function hsl(colorString) {
var _hslFromColorString;
const hslObj = (_hslFromColorString = hslFromColorString(colorString)) != null ? _hslFromColorString : hslFromRgbString(colorString);
export default function hsl(colorString) {
const hslObj = hslFromColorString(colorString) ?? hslFromRgbString(colorString);
if (hslObj === null) throw new Error('Invalid color string');
return hslObj;
}

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

"use strict";
exports.__esModule = true;
exports.default = named;
exports.namedColors = void 0;
const namedColors = {
export const namedColors = {
aliceblue: '#F0F8FF',

@@ -156,6 +151,4 @@ antiquewhite: '#FAEBD7',

};
exports.namedColors = namedColors;
function named(colorName) {
export default function named(colorName) {
return namedColors[colorName.toLowerCase()];
}

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

"use strict";
var _hexString = require("../hexString");
import { isHexString, matchHexString } from '../hexString';
describe('isHexString', () => {

@@ -9,3 +6,3 @@ const valid = ['#33FFAA', '#ff33aa', '#FFaa33', '#33FFAABB', '#33ffaabb', '#3FA', '#3fab'];

it(`returns true for valid hex string: ${str}`, () => {
expect((0, _hexString.isHexString)(str)).toBe(true);
expect(isHexString(str)).toBe(true);
});

@@ -16,3 +13,3 @@ });

it(`returns false for invalid hex string: ${str}`, () => {
expect((0, _hexString.isHexString)(str)).toBe(false);
expect(isHexString(str)).toBe(false);
});

@@ -24,16 +21,16 @@ });

const str = '#FFAACCEE';
expect((0, _hexString.matchHexString)(str)).toEqual(['FF', 'AA', 'CC', 'EE']);
expect(matchHexString(str)).toEqual(['FF', 'AA', 'CC', 'EE']);
});
it('returns only a 3-item array if color has no alpha value', () => {
const str = '#ffaabb';
expect((0, _hexString.matchHexString)(str)).toEqual(['ff', 'aa', 'bb']);
expect(matchHexString(str)).toEqual(['ff', 'aa', 'bb']);
});
it('captures shorthand RGB and alpha hex values', () => {
const str = '#FACE';
expect((0, _hexString.matchHexString)(str)).toEqual(['F', 'A', 'C', 'E']);
expect(matchHexString(str)).toEqual(['F', 'A', 'C', 'E']);
});
it('returns null if string is not a valid hex string', () => {
const str = 'rgb(127, 255, 64)';
expect((0, _hexString.matchHexString)(str)).toBeNull();
expect(matchHexString(str)).toBeNull();
});
});

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

"use strict";
var _hslString = require("../hslString");
import { isHslString, matchHslString } from '../hslString';
describe('isHslString', () => {

@@ -9,3 +6,3 @@ const valid = ['hsl(240, 100%, 50%)', 'hsl(240, 100%, 50%, 0.1)', 'hsl(240, 100%, 50%, 10%)', 'hsl(240,100%,50%,0.1)', 'hsl(180deg, 100%, 50%, 0.1)', 'hsl(3.14rad, 100%, 50%, 0.1)', 'hsl(200grad, 100%, 50%, 0.1)', 'hsl(0.5turn, 100%, 50%, 0.1)', 'hsl(480, 100.5%, -50%, 10)', 'hsl(240 100% 50%)', 'hsl(240 100% 50% / 0.1)', 'hsl(240 100% 50% / 10%)', 'hsl(240deg 100% 50% / 0.1)', 'hsl(240 100% 50%/0.1)', 'hsla(240, 100%, 50%)', 'hsla(240, 100%, 50%, 0.1)', 'HSL(240Deg, 100%, 50%)'];

it(`returns true for valid hsl string: ${str}`, () => {
expect((0, _hslString.isHslString)(str)).toBe(true);
expect(isHslString(str)).toBe(true);
});

@@ -16,3 +13,3 @@ });

it(`returns false for invalid hsl string: ${str}`, () => {
expect((0, _hslString.isHslString)(str)).toBe(false);
expect(isHslString(str)).toBe(false);
});

@@ -24,7 +21,7 @@ });

const str = 'hsl(240, 100%, 50%, 0.1)';
expect((0, _hslString.matchHslString)(str)).toEqual(['240', '100', '50', '0.1']);
expect(matchHslString(str)).toEqual(['240', '100', '50', '0.1']);
});
it('captures percentage opacity', () => {
const str = 'hsl(240, 100%, 50%, 10%)';
expect((0, _hslString.matchHslString)(str)).toEqual(['240', '100', '50', '10%']);
expect(matchHslString(str)).toEqual(['240', '100', '50', '10%']);
});

@@ -35,3 +32,3 @@ const hueWithUnit = ['180deg', '3.14rad', '200grad', '0.5turn'];

const str = `hsl(${hue} 100% 50% / 0.1)`;
expect((0, _hslString.matchHslString)(str)).toEqual([hue, '100', '50', '0.1']);
expect(matchHslString(str)).toEqual([hue, '100', '50', '0.1']);
});

@@ -41,12 +38,12 @@ });

const str = 'hsl(-240, -100%, -50%, -0.1)';
expect((0, _hslString.matchHslString)(str)).toEqual(['-240', '-100', '-50', '-0.1']);
expect(matchHslString(str)).toEqual(['-240', '-100', '-50', '-0.1']);
});
it('returns only a 3-item array if color has no alpha value', () => {
const str = 'hsl(240, 100%, 50%)';
expect((0, _hslString.matchHslString)(str)).toEqual(['240', '100', '50']);
expect(matchHslString(str)).toEqual(['240', '100', '50']);
});
it('returns null if string is not a valid hsl string', () => {
const str = 'rgb(127, 255, 64)';
expect((0, _hslString.matchHslString)(str)).toBeNull();
expect(matchHslString(str)).toBeNull();
});
});

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

"use strict";
var _rgbString = require("../rgbString");
import { isRgbString, matchRgbString } from '../rgbString';
describe('isRgbString', () => {

@@ -9,3 +6,3 @@ const valid = ['rgb(127, 255, 64)', 'rgb(127, 255, 64, 0.1)', 'rgb(127, 255, 64, 10%)', 'rgb(50%, 100%, 25%, 0.1)', 'rgb(240,255,64,0.1)', 'rgb(320, 255.5, -64, 10)', 'rgb(127 255 64)', 'rgb(127 255 64 / 0.1)', 'rgb(127 255 64 / 10%)', 'rgb(50% 100% 25% / 0.1)', 'rgb(127 255 64/0.1)', 'rgba(127, 255, 64, 0.1)', 'rgba(127, 255, 64)', 'rgba(127 255 64 / 0.1)', 'RGB(127, 255, 64)'];

it(`returns true for valid rgb string: ${str}`, () => {
expect((0, _rgbString.isRgbString)(str)).toBe(true);
expect(isRgbString(str)).toBe(true);
});

@@ -16,3 +13,3 @@ });

it(`returns false for invalid rgb string: ${str}`, () => {
expect((0, _rgbString.isRgbString)(str)).toBe(false);
expect(isRgbString(str)).toBe(false);
});

@@ -24,24 +21,24 @@ });

const str = 'rgb(127, 255, 64, 0.1)';
expect((0, _rgbString.matchRgbString)(str)).toEqual(['127', '255', '64', '0.1']);
expect(matchRgbString(str)).toEqual(['127', '255', '64', '0.1']);
});
it('captures percentage opacity', () => {
const str = 'rgb(127, 255, 64, 100%)';
expect((0, _rgbString.matchRgbString)(str)).toEqual(['127', '255', '64', '100%']);
expect(matchRgbString(str)).toEqual(['127', '255', '64', '100%']);
});
it('captures percentage values', () => {
const str = 'rgb(50%, 100%, 25%, 0.1)';
expect((0, _rgbString.matchRgbString)(str)).toEqual(['50%', '100%', '25%', '0.1']);
expect(matchRgbString(str)).toEqual(['50%', '100%', '25%', '0.1']);
});
it('captures negative values', () => {
const str = 'rgb(-127, -255, -64, -0.1)';
expect((0, _rgbString.matchRgbString)(str)).toEqual(['-127', '-255', '-64', '-0.1']);
expect(matchRgbString(str)).toEqual(['-127', '-255', '-64', '-0.1']);
});
it('returns only a 3-item array if color has no alpha value', () => {
const str = 'rgb(127, 255, 64)';
expect((0, _rgbString.matchRgbString)(str)).toEqual(['127', '255', '64']);
expect(matchRgbString(str)).toEqual(['127', '255', '64']);
});
it('returns null if string is not a valid rgb string', () => {
const str = 'hsl(240 100% 50%)';
expect((0, _rgbString.matchRgbString)(str)).toBeNull();
expect(matchRgbString(str)).toBeNull();
});
});

@@ -1,9 +0,6 @@

"use strict";
var _utils = require("../utils");
import { alphaSeparatorMatcher, cssNumberMatcher, exact, extractValuesFromMatch, separatorMatcher } from '../utils';
describe('exact', () => {
it('returns a new regex that is restricted to exact matches only', () => {
const regex = /[0-9]+/;
const exactRegex = (0, _utils.exact)(regex);
const exactRegex = exact(regex);
expect(regex.test(' 123 ')).toBe(true);

@@ -15,3 +12,3 @@ expect(exactRegex.test(' 123 ')).toBe(false);

const regex = /[a-z]+/i;
expect((0, _utils.exact)(regex).test('Abc')).toBe(true);
expect(exact(regex).test('Abc')).toBe(true);
});

@@ -22,11 +19,11 @@ });

const match = ['#ffaaddee', 'ff', 'aa', 'dd', 'ee'];
expect((0, _utils.extractValuesFromMatch)(match)).toEqual(['ff', 'aa', 'dd', 'ee']);
expect(extractValuesFromMatch(match)).toEqual(['ff', 'aa', 'dd', 'ee']);
});
it('removes undefined items', () => {
const match = ['#ffaadd', 'ff', 'aa', 'dd', undefined];
expect((0, _utils.extractValuesFromMatch)(match)).toEqual(['ff', 'aa', 'dd']);
expect(extractValuesFromMatch(match)).toEqual(['ff', 'aa', 'dd']);
});
});
describe('cssNumberMatcher', () => {
const matcher = (0, _utils.exact)(_utils.cssNumberMatcher);
const matcher = exact(cssNumberMatcher);
const valid = ['255', '4.5', '0.1', '.1', '007', '1.000', '-255', '+255', '1.28e+2', '1.28E-2', '-01.2800e+02'];

@@ -46,3 +43,3 @@ valid.forEach(str => {

describe('separatorMatcher', () => {
const matcher = (0, _utils.exact)(_utils.separatorMatcher);
const matcher = exact(separatorMatcher);
const valid = [',', ' ,', ', ', ' , ', ' '];

@@ -62,3 +59,3 @@ valid.forEach(str => {

describe('alphaSeparatorMatcher', () => {
const matcher = (0, _utils.exact)(_utils.alphaSeparatorMatcher);
const matcher = exact(alphaSeparatorMatcher);
const valid = [',', ', ', ' ,', ' , ', '/', '/ ', ' /', ' / '];

@@ -65,0 +62,0 @@ valid.forEach(str => {

@@ -1,26 +0,11 @@

"use strict";
exports.__esModule = true;
exports.hexColorMatcher = void 0;
exports.isHexString = isHexString;
exports.matchHexString = matchHexString;
exports.shortHexColorMatcher = void 0;
var _utils = require("./utils");
import { exact, extractValuesFromMatch } from './utils';
const hex = /[0-9a-fA-F]/.source;
const hexColorMatcher = new RegExp(`#(${hex}{2})(${hex}{2})(${hex}{2})(${hex}{2})?`);
exports.hexColorMatcher = hexColorMatcher;
const shortHexColorMatcher = new RegExp(`#(${hex})(${hex})(${hex})(${hex})?`);
exports.shortHexColorMatcher = shortHexColorMatcher;
function isHexString(colorString) {
return (0, _utils.exact)(hexColorMatcher).test(colorString) || (0, _utils.exact)(shortHexColorMatcher).test(colorString);
export const hexColorMatcher = new RegExp(`#(${hex}{2})(${hex}{2})(${hex}{2})(${hex}{2})?`);
export const shortHexColorMatcher = new RegExp(`#(${hex})(${hex})(${hex})(${hex})?`);
export function isHexString(colorString) {
return exact(hexColorMatcher).test(colorString) || exact(shortHexColorMatcher).test(colorString);
}
function matchHexString(colorString) {
var _exact$exec;
const match = (_exact$exec = (0, _utils.exact)(hexColorMatcher).exec(colorString)) != null ? _exact$exec : (0, _utils.exact)(shortHexColorMatcher).exec(colorString);
return match ? (0, _utils.extractValuesFromMatch)(match) : null;
export function matchHexString(colorString) {
const match = exact(hexColorMatcher).exec(colorString) ?? exact(shortHexColorMatcher).exec(colorString);
return match ? extractValuesFromMatch(match) : null;
}

@@ -1,23 +0,12 @@

"use strict";
exports.__esModule = true;
exports.hslMatcher = void 0;
exports.isHslString = isHslString;
exports.matchHslString = matchHslString;
var _utils = require("./utils");
const num = _utils.cssNumberMatcher.source;
const sep = _utils.separatorMatcher.source;
const asep = _utils.alphaSeparatorMatcher.source;
const hslMatcher = new RegExp(`hsla?\\(\\s*(${num}(?:deg|rad|grad|turn)?)${sep}(${num})%${sep}(${num})%(?:${asep}(${num}%?))?\\s*\\)`, 'i');
exports.hslMatcher = hslMatcher;
function isHslString(colorString) {
return (0, _utils.exact)(hslMatcher).test(colorString);
import { alphaSeparatorMatcher, cssNumberMatcher, exact, extractValuesFromMatch, separatorMatcher } from './utils';
const num = cssNumberMatcher.source;
const sep = separatorMatcher.source;
const asep = alphaSeparatorMatcher.source;
export const hslMatcher = new RegExp(`hsla?\\(\\s*(${num}(?:deg|rad|grad|turn)?)${sep}(${num})%${sep}(${num})%(?:${asep}(${num}%?))?\\s*\\)`, 'i');
export function isHslString(colorString) {
return exact(hslMatcher).test(colorString);
}
function matchHslString(colorString) {
const match = (0, _utils.exact)(hslMatcher).exec(colorString);
return match ? (0, _utils.extractValuesFromMatch)(match) : null;
export function matchHslString(colorString) {
const match = exact(hslMatcher).exec(colorString);
return match ? extractValuesFromMatch(match) : null;
}

@@ -1,23 +0,12 @@

"use strict";
exports.__esModule = true;
exports.isRgbString = isRgbString;
exports.matchRgbString = matchRgbString;
exports.rgbMatcher = void 0;
var _utils = require("./utils");
const num = _utils.cssNumberMatcher.source;
const sep = _utils.separatorMatcher.source;
const asep = _utils.alphaSeparatorMatcher.source;
const rgbMatcher = new RegExp(`rgba?\\(\\s*(${num}%?)${sep}(${num}%?)${sep}(${num}%?)(?:${asep}(${num}%?))?\\s*\\)`, 'i');
exports.rgbMatcher = rgbMatcher;
function isRgbString(colorString) {
return (0, _utils.exact)(rgbMatcher).test(colorString);
import { alphaSeparatorMatcher, cssNumberMatcher, exact, extractValuesFromMatch, separatorMatcher } from './utils';
const num = cssNumberMatcher.source;
const sep = separatorMatcher.source;
const asep = alphaSeparatorMatcher.source;
export const rgbMatcher = new RegExp(`rgba?\\(\\s*(${num}%?)${sep}(${num}%?)${sep}(${num}%?)(?:${asep}(${num}%?))?\\s*\\)`, 'i');
export function isRgbString(colorString) {
return exact(rgbMatcher).test(colorString);
}
function matchRgbString(colorString) {
const match = (0, _utils.exact)(rgbMatcher).exec(colorString);
return match ? (0, _utils.extractValuesFromMatch)(match) : null;
export function matchRgbString(colorString) {
const match = exact(rgbMatcher).exec(colorString);
return match ? extractValuesFromMatch(match) : null;
}

@@ -1,22 +0,9 @@

"use strict";
exports.__esModule = true;
exports.cssNumberMatcher = exports.alphaSeparatorMatcher = void 0;
exports.exact = exact;
exports.extractValuesFromMatch = extractValuesFromMatch;
exports.separatorMatcher = void 0;
function exact(regex) {
export function exact(regex) {
return new RegExp(`^${regex.source}$`, regex.flags);
}
function extractValuesFromMatch(match) {
export function extractValuesFromMatch(match) {
return match.slice(1).filter(val => val !== undefined);
}
const cssNumberMatcher = /[+-]?(?=\.\d|\d)\d*(?:\.\d+)?(?:[eE][+-]?\d+)?/;
exports.cssNumberMatcher = cssNumberMatcher;
const separatorMatcher = /(?=[,\s])\s*(?:,\s*)?/;
exports.separatorMatcher = separatorMatcher;
const alphaSeparatorMatcher = /\s*[,\/]\s*/;
exports.alphaSeparatorMatcher = alphaSeparatorMatcher;
export const cssNumberMatcher = /[+-]?(?=\.\d|\d)\d*(?:\.\d+)?(?:[eE][+-]?\d+)?/;
export const separatorMatcher = /(?=[,\s])\s*(?:,\s*)?/;
export const alphaSeparatorMatcher = /\s*[,\/]\s*/;

@@ -1,33 +0,16 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = rgb;
exports.isRgb = isRgb;
exports.normalizeRgb = normalizeRgb;
exports.rgbFromColorString = rgbFromColorString;
var _utils = require("../utils");
var _hsl = require("./hsl");
var _named = _interopRequireDefault(require("./named"));
var _hexString = require("./parsers/hexString");
var _rgbString = require("./parsers/rgbString");
var _hslToRgb = _interopRequireDefault(require("./transforms/hslToRgb"));
function isRgb(color) {
import { clamp } from '../utils';
import { hslFromColorString } from './hsl';
import named from './named';
import { matchHexString } from './parsers/hexString';
import { matchRgbString } from './parsers/rgbString';
import hslToRgb from './transforms/hslToRgb';
export function isRgb(color) {
return typeof color.r === 'number' && typeof color.g === 'number' && typeof color.b === 'number' && typeof color.a === 'number';
}
function normalizeRgb(rgb) {
export function normalizeRgb(rgb) {
return {
r: (0, _utils.clamp)(rgb.r, 0, 255),
g: (0, _utils.clamp)(rgb.g, 0, 255),
b: (0, _utils.clamp)(rgb.b, 0, 255),
a: (0, _utils.clamp)(rgb.a, 0, 1)
r: clamp(rgb.r, 0, 255),
g: clamp(rgb.g, 0, 255),
b: clamp(rgb.b, 0, 255),
a: clamp(rgb.a, 0, 1)
};

@@ -37,4 +20,2 @@ }

function rgbFromParsedHexString(match) {
var _rgbValues$;
const rgbValues = match.map(val => {

@@ -47,3 +28,3 @@ if (val.length === 1) {

});
const alpha = ((_rgbValues$ = rgbValues[3]) != null ? _rgbValues$ : 255) / 255;
const alpha = (rgbValues[3] ?? 255) / 255;
return {

@@ -58,4 +39,2 @@ r: rgbValues[0],

function rgbFromParsedRgbString(match) {
var _rgbValues$2;
const rgbValues = match.map((val, index) => {

@@ -78,3 +57,3 @@ let num = parseFloat(val);

b: rgbValues[2],
a: (_rgbValues$2 = rgbValues[3]) != null ? _rgbValues$2 : 1
a: rgbValues[3] ?? 1
});

@@ -84,7 +63,7 @@ }

function rgbFromHslString(colorString) {
const hslColor = (0, _hsl.hslFromColorString)(colorString);
return hslColor ? (0, _hslToRgb.default)(hslColor) : null;
const hslColor = hslFromColorString(colorString);
return hslColor ? hslToRgb(hslColor) : null;
}
function rgbFromColorString(colorString) {
export function rgbFromColorString(colorString) {
colorString = colorString.trim();

@@ -97,3 +76,3 @@ if (colorString.toLowerCase() === 'transparent') return {

};
const hexFromName = (0, _named.default)(colorString);
const hexFromName = named(colorString);

@@ -105,12 +84,9 @@ if (hexFromName) {

let match;
if ((match = (0, _hexString.matchHexString)(colorString)) !== null) return rgbFromParsedHexString(match);else if ((match = (0, _rgbString.matchRgbString)(colorString)) !== null) return rgbFromParsedRgbString(match);
if ((match = matchHexString(colorString)) !== null) return rgbFromParsedHexString(match);else if ((match = matchRgbString(colorString)) !== null) return rgbFromParsedRgbString(match);
return null;
}
function rgb(colorString) {
var _rgbFromColorString;
const rgbObj = (_rgbFromColorString = rgbFromColorString(colorString)) != null ? _rgbFromColorString : rgbFromHslString(colorString);
export default function rgb(colorString) {
const rgbObj = rgbFromColorString(colorString) ?? rgbFromHslString(colorString);
if (rgbObj === null) throw new Error('Invalid color string');
return rgbObj;
}

@@ -1,9 +0,3 @@

"use strict";
exports.__esModule = true;
exports.default = hslToRgb;
var _hsl = require("../hsl");
function hslToRgb(hsl) {
import { normalizeHsl } from '../hsl';
export default function hslToRgb(hsl) {
const {

@@ -14,3 +8,3 @@ h,

a
} = (0, _hsl.normalizeHsl)(hsl);
} = normalizeHsl(hsl);
const hue = h || 0;

@@ -17,0 +11,0 @@ const sat = s / 100;

@@ -1,9 +0,3 @@

"use strict";
exports.__esModule = true;
exports.default = rgbToHsl;
var _rgb = require("../rgb");
function rgbToHsl(rgb) {
import { normalizeRgb } from '../rgb';
export default function rgbToHsl(rgb) {
const {

@@ -14,3 +8,3 @@ r,

a
} = (0, _rgb.normalizeRgb)(rgb);
} = normalizeRgb(rgb);
const red = r / 255;

@@ -17,0 +11,0 @@ const green = g / 255;

@@ -1,17 +0,9 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = colorScheme;
var _colorSet = _interopRequireDefault(require("./colorSet"));
function colorScheme(baseColors, roleMapping, options) {
import colorSet from './colorSet';
export default function colorScheme(baseColors, roleMapping, options) {
const {
colorMapping,
noCache
} = options != null ? options : {};
const colors = (0, _colorSet.default)(baseColors, colorMapping, noCache);
} = options ?? {};
const colors = colorSet(baseColors, colorMapping, noCache);
return roleMapping(colors);
}

@@ -1,19 +0,10 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = colorSet;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/extends"));
var _palette = _interopRequireDefault(require("./palette"));
function colorSet(baseColors, colorMapping, noCache) {
import _extends from "@babel/runtime/helpers/esm/extends";
import palette from './palette';
export default function colorSet(baseColors, colorMapping, noCache) {
const colorClasses = Object.keys(baseColors);
return colorClasses.reduce((palettes, colorClass) => {
return (0, _extends2.default)({}, palettes, {
[colorClass]: (0, _palette.default)(baseColors[colorClass], colorMapping, noCache)
return _extends({}, palettes, {
[colorClass]: palette(baseColors[colorClass], colorMapping, noCache)
});
}, {});
}

@@ -1,76 +0,16 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.triad = exports.saturation = exports.rotation = exports.rgbToHsl = exports.rgb = exports.palette = exports.opacity = exports.normalize = exports.lightness = exports.isRgb = exports.isHsl = exports.hslToRgb = exports.hsl = exports.harmony = exports.complement = exports.colorSet = exports.colorScheme = exports.analogue = void 0;
var _colorScheme = _interopRequireDefault(require("./colorScheme"));
exports.colorScheme = _colorScheme.default;
var _colorSet = _interopRequireDefault(require("./colorSet"));
exports.colorSet = _colorSet.default;
var _normalize = _interopRequireDefault(require("./normalize"));
exports.normalize = _normalize.default;
var _palette = _interopRequireDefault(require("./palette"));
exports.palette = _palette.default;
var _analogue = _interopRequireDefault(require("./mappings/analogue"));
exports.analogue = _analogue.default;
var _complement = _interopRequireDefault(require("./mappings/complement"));
exports.complement = _complement.default;
var _lightness = _interopRequireDefault(require("./mappings/lightness"));
exports.lightness = _lightness.default;
var _opacity = _interopRequireDefault(require("./mappings/opacity"));
exports.opacity = _opacity.default;
var _rotation = _interopRequireDefault(require("./mappings/rotation"));
exports.rotation = _rotation.default;
var _saturation = _interopRequireDefault(require("./mappings/saturation"));
exports.saturation = _saturation.default;
var _triad = _interopRequireDefault(require("./mappings/triad"));
exports.triad = _triad.default;
var _harmony = _interopRequireDefault(require("./presets/harmony"));
exports.harmony = _harmony.default;
var _hsl = _interopRequireWildcard(require("./color/hsl"));
exports.hsl = _hsl.default;
exports.isHsl = _hsl.isHsl;
var _rgb = _interopRequireWildcard(require("./color/rgb"));
exports.rgb = _rgb.default;
exports.isRgb = _rgb.isRgb;
var _hslToRgb = _interopRequireDefault(require("./color/transforms/hslToRgb"));
exports.hslToRgb = _hslToRgb.default;
var _rgbToHsl = _interopRequireDefault(require("./color/transforms/rgbToHsl"));
exports.rgbToHsl = _rgbToHsl.default;
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
export { default as colorScheme } from './colorScheme';
export { default as colorSet } from './colorSet';
export { default as normalize } from './normalize';
export { default as palette } from './palette';
export { default as analogue } from './mappings/analogue';
export { default as complement } from './mappings/complement';
export { default as lightness } from './mappings/lightness';
export { default as opacity } from './mappings/opacity';
export { default as rotation } from './mappings/rotation';
export { default as saturation } from './mappings/saturation';
export { default as triad } from './mappings/triad';
export { default as harmony } from './presets/harmony';
export { default as hsl, isHsl } from './color/hsl';
export { default as rgb, isRgb } from './color/rgb';
export { default as hslToRgb } from './color/transforms/hslToRgb';
export { default as rgbToHsl } from './color/transforms/rgbToHsl';

@@ -1,16 +0,8 @@

"use strict";
import rotation from './rotation';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _rotation = _interopRequireDefault(require("./rotation"));
const analogue = (baseColor, key) => {
if (typeof key !== 'number' || key === 0) return baseColor;
return (0, _rotation.default)(baseColor, 30 * key);
return rotation(baseColor, 30 * key);
};
var _default = analogue;
exports.default = _default;
export default analogue;

@@ -1,10 +0,3 @@

"use strict";
import rotation from './rotation';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _rotation = _interopRequireDefault(require("./rotation"));
const complement = (baseColor, key) => {

@@ -21,6 +14,5 @@ if (typeof key !== 'number' || key === 0) return baseColor;

return (0, _rotation.default)(baseColor, angle);
return rotation(baseColor, angle);
};
var _default = complement;
exports.default = _default;
export default complement;

@@ -1,19 +0,10 @@

"use strict";
import _extends from "@babel/runtime/helpers/esm/extends";
import colorString from '../color/colorString';
import hsl from '../color/hsl';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/extends"));
var _colorString = _interopRequireDefault(require("../color/colorString"));
var _hsl = _interopRequireDefault(require("../color/hsl"));
const lightness = (baseColor, key) => {
if (typeof key !== 'number') return baseColor;
const base = (0, _hsl.default)(baseColor);
const base = hsl(baseColor);
const targetL = Math.min(Math.max(key, 0), 100);
return (0, _colorString.default)((0, _extends2.default)({}, base, {
return colorString(_extends({}, base, {
l: targetL

@@ -23,3 +14,2 @@ }));

var _default = lightness;
exports.default = _default;
export default lightness;

@@ -1,19 +0,10 @@

"use strict";
import _extends from "@babel/runtime/helpers/esm/extends";
import colorString from '../color/colorString';
import rgb from '../color/rgb';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/extends"));
var _colorString = _interopRequireDefault(require("../color/colorString"));
var _rgb = _interopRequireDefault(require("../color/rgb"));
const opacity = (baseColor, key) => {
if (typeof key !== 'number') return baseColor;
const base = (0, _rgb.default)(baseColor);
const base = rgb(baseColor);
const targetA = Math.min(Math.max(key, 0), 1);
return (0, _colorString.default)((0, _extends2.default)({}, base, {
return colorString(_extends({}, base, {
a: targetA

@@ -23,3 +14,2 @@ }));

var _default = opacity;
exports.default = _default;
export default opacity;

@@ -1,19 +0,10 @@

"use strict";
import _extends from "@babel/runtime/helpers/esm/extends";
import colorString from '../color/colorString';
import hsl from '../color/hsl';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/extends"));
var _colorString = _interopRequireDefault(require("../color/colorString"));
var _hsl = _interopRequireDefault(require("../color/hsl"));
const rotation = (baseColor, key) => {
if (typeof key !== 'number' || key === 0) return baseColor;
const base = (0, _hsl.default)(baseColor);
const base = hsl(baseColor);
const targetH = (base.h + key) % 360;
return (0, _colorString.default)((0, _extends2.default)({}, base, {
return colorString(_extends({}, base, {
h: targetH

@@ -23,3 +14,2 @@ }));

var _default = rotation;
exports.default = _default;
export default rotation;

@@ -1,19 +0,10 @@

"use strict";
import _extends from "@babel/runtime/helpers/esm/extends";
import colorString from '../color/colorString';
import hsl from '../color/hsl';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/extends"));
var _colorString = _interopRequireDefault(require("../color/colorString"));
var _hsl = _interopRequireDefault(require("../color/hsl"));
const saturation = (baseColor, key) => {
if (typeof key !== 'number') return baseColor;
const base = (0, _hsl.default)(baseColor);
const base = hsl(baseColor);
const targetS = Math.min(Math.max(key, 0), 100);
return (0, _colorString.default)((0, _extends2.default)({}, base, {
return colorString(_extends({}, base, {
s: targetS

@@ -23,3 +14,2 @@ }));

var _default = saturation;
exports.default = _default;
export default saturation;

@@ -1,16 +0,8 @@

"use strict";
import rotation from './rotation';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _rotation = _interopRequireDefault(require("./rotation"));
const triad = (baseColor, key) => {
if (typeof key !== 'number' || key === 0) return baseColor;
return (0, _rotation.default)(baseColor, 120 * key);
return rotation(baseColor, 120 * key);
};
var _default = triad;
exports.default = _default;
export default triad;

@@ -1,14 +0,5 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = normalize;
var _colorString = _interopRequireDefault(require("./color/colorString"));
var _rgb = _interopRequireDefault(require("./color/rgb"));
function normalize(color) {
return (0, _colorString.default)((0, _rgb.default)(color));
import colorString from './color/colorString';
import rgb from './color/rgb';
export default function normalize(color) {
return colorString(rgb(color));
}

@@ -1,11 +0,3 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = palette;
var _lightness = _interopRequireDefault(require("./mappings/lightness"));
function palette(baseColor, colorMapping = _lightness.default, noCache) {
import lightness from './mappings/lightness';
export default function palette(baseColor, colorMapping = lightness, noCache) {
const cache = {};

@@ -12,0 +4,0 @@

@@ -1,22 +0,12 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = harmony;
var _rotation = _interopRequireDefault(require("../mappings/rotation"));
var _saturation = _interopRequireDefault(require("../mappings/saturation"));
var _normalize = _interopRequireDefault(require("../normalize"));
function harmony(baseColor) {
import rotation from '../mappings/rotation';
import saturation from '../mappings/saturation';
import normalize from '../normalize';
export default function harmony(baseColor) {
return {
primary: (0, _normalize.default)(baseColor),
secondary: (0, _saturation.default)((0, _rotation.default)(baseColor, 30), 15),
accent: (0, _saturation.default)((0, _rotation.default)(baseColor, 90), 80),
neutral: (0, _saturation.default)(baseColor, 5),
primary: normalize(baseColor),
secondary: saturation(rotation(baseColor, 30), 15),
accent: saturation(rotation(baseColor, 90), 80),
neutral: saturation(baseColor, 5),
error: '#BB2211'
};
}

@@ -1,8 +0,3 @@

"use strict";
exports.__esModule = true;
exports.clamp = clamp;
function clamp(value, min, max) {
export function clamp(value, min, max) {
return Math.max(min, Math.min(value, max));
}
{
"name": "simpler-color",
"version": "0.3.2",
"version": "1.0.0",
"description": "Simpler Color - Create your own complete color system fast and easy!",

@@ -5,0 +5,0 @@ "keywords": [

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