color-catalogs
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -25,3 +25,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HEXtoRGB = exports.translateByName = exports.translateByColor = exports.LANGUAGES = exports.colorDistance = exports.findColorByName = exports.findByColor = exports.COLORS = void 0; | ||
exports.HEXtoRGB = exports.translateByName = exports.translateByColor = exports.LANGUAGES = exports.groupColors = exports.colorDistance = exports.findColorByName = exports.findByColor = exports.COLORS = void 0; | ||
var colors_1 = __importDefault(require("./src/colors")); | ||
@@ -34,2 +34,3 @@ exports.COLORS = colors_1.default; | ||
Object.defineProperty(exports, "HEXtoRGB", { enumerable: true, get: function () { return utils_1.HEXtoRGB; } }); | ||
Object.defineProperty(exports, "groupColors", { enumerable: true, get: function () { return utils_1.groupColors; } }); | ||
var languages_1 = __importStar(require("./src/languages")); | ||
@@ -36,0 +37,0 @@ exports.LANGUAGES = languages_1.default; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.colorDistance = exports.HEXtoRGB = exports.findByColor = exports.findColorByName = void 0; | ||
exports.groupColors = exports.colorDistance = exports.HEXtoRGB = exports.findByColor = exports.findColorByName = void 0; | ||
var colors_1 = __importDefault(require("./colors")); | ||
@@ -35,5 +35,2 @@ function findColorByName(catalog, name) { | ||
exports.findByColor = findByColor; | ||
function mix(a, b, v) { | ||
return (1 - v) * a + v * b; | ||
} | ||
function HEXtoRGB(hex) { | ||
@@ -67,2 +64,7 @@ hex = hex.replace(/#/g, ''); | ||
return null; | ||
// If colors is a are string, convert to RGB array | ||
if (typeof v1 === 'string') | ||
v1 = HEXtoRGB(v1); | ||
if (typeof v2 === 'string') | ||
v2 = HEXtoRGB(v2); | ||
// If colors are {r,g,b}, convert o array | ||
@@ -81,2 +83,44 @@ if (v1.r) | ||
; | ||
function groupColors(colors, rangeNumber) { | ||
// Auto calculates size based on colors length | ||
if (!rangeNumber) { | ||
var defaultRange = 10; | ||
for (var i = 20; i >= defaultRange; i--) { | ||
if (colors.length % i === 0) { | ||
rangeNumber = i; | ||
break; | ||
} | ||
} | ||
if (!rangeNumber) { | ||
rangeNumber = defaultRange; | ||
} | ||
} | ||
var distances = []; | ||
var first = colors[0]; | ||
var min, max; | ||
for (var i = 1; i < colors.length; i++) { | ||
var color = colors[i]; | ||
var distance = colorDistance(first, color); | ||
if (!distance) | ||
continue; | ||
var data = { distance: distance, color: color }; | ||
if (!min || min.distance < distance) | ||
min = data; | ||
if (!max || max.distance > distance) | ||
max = data; | ||
distances.push(data); | ||
} | ||
distances = distances.sort(function (a, b) { return a.distance > b.distance ? 1 : -1; }).map(function (a) { return a.color; }); | ||
var range = distances.length / rangeNumber; | ||
var groupedColors = []; | ||
for (var i = 0; i < rangeNumber; i++) { | ||
var rangeColors = distances.slice(i * range, (i + 1) * range); | ||
groupedColors.push(rangeColors); | ||
} | ||
var total = groupedColors.reduce(function (acc, val) { return acc + val.length; }, 0); | ||
if (total !== distances.length) | ||
return false; | ||
return groupedColors; | ||
} | ||
exports.groupColors = groupColors; | ||
//# sourceMappingURL=utils.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
describe('Color Utils Test', function () { | ||
// it('findByColor', () => { | ||
// const keys = Object.keys(COLORS.Australia) | ||
// expect(keys.length).to.be.equal(206) | ||
// const color = findByColor('Australia', '#cc00cc') | ||
// expect(color).to.eql({ name: 'P41 - Erica Pink', value: '#c55a83' }); | ||
// }); | ||
// it('findColorByName', () => { | ||
// const keys = Object.keys(COLORS.British4800) | ||
// expect(keys.length).to.be.equal(122) | ||
// const color = findColorByName('British4800', '#cc00cc') | ||
// expect(color).to.eql({ name: '24 E 53 - Purple', value: '#90538f' }); | ||
// }); | ||
// it('colorDistance', () => { | ||
// const keys = Object.keys(COLORS.BritishStandard) | ||
// expect(keys.length).to.be.equal(119) | ||
// const color = colorDistance('BritishStandard', '#cc00cc') | ||
// expect(color).to.eql({ name: '381 542 - Ruby', value: '#982d57' }); | ||
// }); | ||
// it('HEXtoRGB', () => { | ||
// const keys = Object.keys(COLORS.Crayola72) | ||
// expect(keys.length).to.be.equal(72) | ||
// const color = HEXtoRGB('#cc00cc') | ||
// expect(color).to.eql({ name: 'Hot Pink', value: '#C22AA3' }); | ||
// }); | ||
var index_1 = require("../index"); | ||
var chai_1 = require("chai"); | ||
describe.only('Color Utils Test', function () { | ||
it('groupColors with size', function () { | ||
var size = 5; | ||
var colors = Object.values(index_1.COLORS.Australia); | ||
var grouped = index_1.groupColors(colors, size); | ||
chai_1.expect(grouped.length).to.eql(size); | ||
chai_1.expect(grouped[0].length).to.eql(Math.floor(colors.length / size)); | ||
}); | ||
it('groupColors without size', function () { | ||
var colors = Object.values(index_1.COLORS.Australia); | ||
var grouped = index_1.groupColors(colors); | ||
var size = 10; | ||
chai_1.expect(grouped.length).to.eql(size); | ||
chai_1.expect(grouped[0].length).to.eql(Math.floor(colors.length / size)); | ||
}); | ||
it('findByColor', function () { | ||
var catalog = index_1.COLORS.Australia; | ||
var keys = Object.keys(catalog); | ||
var color = index_1.findByColor('Australia', catalog[keys[0]]); | ||
chai_1.expect(color).to.eql({ name: 'B61 - Coral Sea', value: '#2b3873' }); | ||
}); | ||
it('findColorByName', function () { | ||
var catalog = index_1.COLORS.British4800; | ||
var keys = Object.keys(catalog); | ||
var color = index_1.findColorByName('British4800', keys[0]); | ||
chai_1.expect(color).to.eql('#deded2'); | ||
}); | ||
it('colorDistance', function () { | ||
var catalog = index_1.COLORS.BritishStandard; | ||
var keys = Object.keys(catalog); | ||
chai_1.expect(keys.length).to.be.equal(119); | ||
var color = index_1.colorDistance(catalog[keys[0]], catalog[keys[1]]); | ||
chai_1.expect(color).to.eql(77.47902942086975); | ||
}); | ||
it('HEXtoRGB', function () { | ||
var catalog = index_1.COLORS.Crayola72; | ||
var keys = Object.keys(catalog); | ||
var color = index_1.HEXtoRGB(catalog[keys[0]]); | ||
chai_1.expect(color).to.eql([115, 97, 71]); | ||
}); | ||
}); | ||
//# sourceMappingURL=utils.js.map |
import COLORS from './src/colors' | ||
import { | ||
findByColor, findColorByName, | ||
colorDistance, HEXtoRGB | ||
colorDistance, HEXtoRGB, groupColors | ||
} from './src/utils' | ||
@@ -15,2 +15,3 @@ import LANGUAGES, { translateByColor, translateByName } from './src/languages' | ||
colorDistance, | ||
groupColors, | ||
@@ -17,0 +18,0 @@ // i18n |
{ | ||
"name": "color-catalogs", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A lot of Color Catalogs with translations, color-pickers integration and utils functions", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# color-catalogs | ||
[![Build Status](https://app.travis-ci.com/mariohmol/color-catalogs.svg?branch=master)](https://app.travis-ci.com/mariohmol/color-catalogs) | ||
@@ -3,0 +4,0 @@ Find good color catalog for your business, let the users pick up colors and find the color closer based on name or RGB/Hash |
import COLORS from './colors' | ||
export function findColorByName(catalog: string, name: any){ | ||
if(!COLORS[catalog]) return | ||
if(!name) return COLORS[catalog] | ||
export function findColorByName(catalog: string, name: any) { | ||
if (!COLORS[catalog]) return | ||
if (!name) return COLORS[catalog] | ||
return COLORS[catalog][name] | ||
} | ||
export function findByColor(catalog: string, color: any){ | ||
if(!COLORS[catalog]) return | ||
export function findByColor(catalog: string, color: any) { | ||
if (!COLORS[catalog]) return | ||
const catalogFound: any = COLORS[catalog] | ||
const colorRGB = HEXtoRGB(color) | ||
const colorRGB = HEXtoRGB(color) | ||
let minValue, minColor: any | ||
for(const color in catalogFound){ | ||
for (const color in catalogFound) { | ||
const value = catalogFound[color] | ||
const rgb = HEXtoRGB(value) | ||
const rgb = HEXtoRGB(value) | ||
const distance = colorDistance(rgb, colorRGB) | ||
if(!minValue || (distance && minValue > distance)){ | ||
if (!minValue || (distance && minValue > distance)) { | ||
minValue = distance | ||
@@ -26,6 +26,2 @@ minColor = color | ||
function mix(a: any, b: any, v: any) { | ||
return (1 - v) * a + v * b; | ||
} | ||
export function HEXtoRGB(hex: string) { | ||
@@ -57,6 +53,12 @@ hex = hex.replace(/#/g, ''); | ||
export function colorDistance(v1: any, v2: any) { | ||
if(!v1 || !v2) return null | ||
if (!v1 || !v2) return null | ||
// If colors is a are string, convert to RGB array | ||
if (typeof v1 === 'string') v1 = HEXtoRGB(v1) | ||
if (typeof v2 === 'string') v2 = HEXtoRGB(v2) | ||
// If colors are {r,g,b}, convert o array | ||
if (v1.r) v1 = [v1.r, v1.g, v1.b] | ||
if (v2.r) v2 = [v2.r, v2.g, v2.b] | ||
var i, | ||
@@ -70,1 +72,43 @@ d = 0; | ||
}; | ||
export function groupColors(colors: Array<string>, rangeNumber?: number) { | ||
// Auto calculates size based on colors length | ||
if (!rangeNumber) { | ||
const defaultRange = 10 | ||
for (let i = 20; i >= defaultRange; i--) { | ||
if (colors.length % i === 0) { | ||
rangeNumber = i | ||
break | ||
} | ||
} | ||
if (!rangeNumber) { | ||
rangeNumber = defaultRange | ||
} | ||
} | ||
let distances = [] | ||
const first = colors[0] | ||
let min, max | ||
for (let i = 1; i < colors.length; i++) { | ||
const color = colors[i] | ||
const distance = colorDistance(first, color) | ||
if (!distance) continue | ||
const data: any = { distance, color } | ||
if (!min || min.distance < distance) min = data | ||
if (!max || max.distance > distance) max = data | ||
distances.push(data) | ||
} | ||
distances = distances.sort((a, b) => a.distance > b.distance ? 1 : -1).map(a => a.color) | ||
const range = distances.length / rangeNumber | ||
const groupedColors = [] | ||
for (let i = 0; i < rangeNumber; i++) { | ||
const rangeColors = distances.slice(i * range, (i + 1) * range) | ||
groupedColors.push(rangeColors) | ||
} | ||
const total = groupedColors.reduce((acc, val) => acc + val.length, 0) | ||
if (total !== distances.length) return false | ||
return groupedColors | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6592977
189228
145