gradient-color
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -0,1 +1,6 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
@@ -5,5 +10,5 @@ * Stupid error collections | ||
exports.NO_EDN_FRAC = new Error('The last element in the color array should not have a frac field'); | ||
exports.MIN_ARRAY_LENGTH = new Error('Color array length must > 1'); | ||
exports.FRAC_SUM_ERROR = new Error('The sum of the fracs should = 1'); | ||
exports.COLOR_NUMBER_ERROR = new Error('The number of generated colors should >= the number of color stops'); | ||
var NO_EDN_FRAC = exports.NO_EDN_FRAC = new Error('The last element in the color array should not have a frac field'); | ||
var MIN_ARRAY_LENGTH = exports.MIN_ARRAY_LENGTH = new Error('Color array length must > 1'); | ||
var FRAC_SUM_ERROR = exports.FRAC_SUM_ERROR = new Error('The sum of the fracs should = 1'); | ||
var COLOR_NUMBER_ERROR = exports.COLOR_NUMBER_ERROR = new Error('The number of generated colors should >= the number of color stops'); |
@@ -1,14 +0,20 @@ | ||
const Color = require('color'); | ||
const { | ||
NO_EDN_FRAC, | ||
MIN_ARRAY_LENGTH, | ||
FRAC_SUM_ERROR, | ||
COLOR_NUMBER_ERROR | ||
} = require('./errors'); | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _color = require('color'); | ||
var _color2 = _interopRequireDefault(_color); | ||
var _errors = require('./errors'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Generate n colors with given color stops | ||
* | ||
* @param {Array} colorArray | ||
* @param {Number} n number of colors that need to generate | ||
* | ||
* @param {Array} colorArray | ||
* @param {Number} n number of colors that need to generate | ||
* @returns {Array} array of generated colors in rgb mode | ||
@@ -18,26 +24,26 @@ */ | ||
function gradient(colorArray, n) { | ||
const isFullOption = checkParam(colorArray, n); | ||
var isFullOption = checkParam(colorArray, n); | ||
// init 2 arrays for algorithm | ||
let colorList = []; | ||
let fracList = []; | ||
var colorList = []; | ||
var fracList = []; | ||
// result array for storing data | ||
let resultArray = []; | ||
var resultArray = []; | ||
// simple array of color string | ||
if (!isFullOption) { | ||
const frac = 1 / (colorArray.length - 1); | ||
var frac = 1 / (colorArray.length - 1); | ||
colorArray.forEach((colorString, index) => { | ||
colorArray.forEach(function (colorString, index) { | ||
if (index !== colorArray.length - 1) { | ||
colorList.push(Color(colorString)); | ||
colorList.push((0, _color2.default)(colorString)); | ||
fracList.push(frac); | ||
} else { | ||
colorList.push(Color(colorString)); | ||
colorList.push((0, _color2.default)(colorString)); | ||
} | ||
}); | ||
} else { | ||
colorArray.forEach((obj, index) => { | ||
colorArray.forEach(function (obj, index) { | ||
if (index !== colorArray.length - 1) { | ||
colorList.push(Color(obj.color)); | ||
colorList.push((0, _color2.default)(obj.color)); | ||
fracList.push(obj.frac); | ||
@@ -47,6 +53,6 @@ } else { | ||
// the last item could be like { color: #ffffff } | ||
colorList.push(Color(obj.color)); | ||
colorList.push((0, _color2.default)(obj.color)); | ||
} else { | ||
// and it could also be like '#ffffff' | ||
colorList.push(Color(obj)); | ||
colorList.push((0, _color2.default)(obj)); | ||
} | ||
@@ -57,7 +63,9 @@ } | ||
const assignList = assignNumbers(fracList, n); | ||
var assignList = assignNumbers(fracList, n); | ||
resultArray = createGradient(colorList, assignList); | ||
// convert colors to string version | ||
resultArray = resultArray.map(c => c.rgb().toString()); | ||
resultArray = resultArray.map(function (c) { | ||
return c.rgb().toString(); | ||
}); | ||
@@ -71,8 +79,8 @@ return resultArray; | ||
* * -> generated color | ||
* | ||
* | ||
* o * * * | o * * * * | o * * o -> generated color list in char version | ||
* 4 5 4 -> assigned number of colors need to be generated | ||
* | ||
* | ||
* The last section, the end color should be considered in the generated colors | ||
* | ||
* | ||
* @returns {Array} array of colors in Color(pkg) format, need toString() call | ||
@@ -82,11 +90,11 @@ */ | ||
function createGradient(colorList, assignList) { | ||
let result = []; | ||
var result = []; | ||
assignList.forEach((num, index) => { | ||
const isLastElement = index === assignList.length - 1; | ||
const list = []; | ||
assignList.forEach(function (num, index) { | ||
var isLastElement = index === assignList.length - 1; | ||
var list = []; | ||
// get end point color | ||
const start = colorList[index]; | ||
const end = colorList[index + 1]; | ||
var start = colorList[index]; | ||
var end = colorList[index + 1]; | ||
@@ -99,13 +107,13 @@ // if last element, end color should be in the list, | ||
const deltaR = (end.red() - start.red()) / num; | ||
const deltaG = (end.green() - start.green()) / num; | ||
const deltaB = (end.blue() - start.blue()) / num; | ||
var deltaR = (end.red() - start.red()) / num; | ||
var deltaG = (end.green() - start.green()) / num; | ||
var deltaB = (end.blue() - start.blue()) / num; | ||
// generate num colors | ||
for (let i = 0; i < num; i++) { | ||
const R = start.red() + i * deltaR; | ||
const G = start.green() + i * deltaG; | ||
const B = start.blue() + i * deltaB; | ||
for (var i = 0; i < num; i++) { | ||
var R = start.red() + i * deltaR; | ||
var G = start.green() + i * deltaG; | ||
var B = start.blue() + i * deltaB; | ||
list.push(Color.rgb(R, G, B)); | ||
list.push(_color2.default.rgb(R, G, B)); | ||
} | ||
@@ -126,19 +134,19 @@ | ||
* Calculate and optimize the number of each color period | ||
* | ||
* | ||
* Sometimes frac * N might be a fraction | ||
* So we use this algorithm: | ||
* | ||
* | ||
* 1. Split the number into 2 parts, each part fits in an array: | ||
* [2, 4, 1, 5] -> int array | ||
* [0.2, 0.5, 0.9, 0.3] -> decimal array | ||
* | ||
* | ||
* The left number should be: | ||
* left = N - sum(intArray) | ||
* | ||
* | ||
* 2. Sort the decimal array from large to small, assign left to | ||
* the corresponding element in intArray one by one | ||
* until left === 0 | ||
* | ||
* | ||
* 3. There goes your final array! | ||
* | ||
* | ||
* @returns {Array} array of optimized color numbers | ||
@@ -148,10 +156,10 @@ */ | ||
function assignNumbers(fracList, n) { | ||
const intArray = []; | ||
const decimalArray = []; | ||
var intArray = []; | ||
var decimalArray = []; | ||
// assign int part | ||
fracList.forEach((frac, index) => { | ||
const real = frac * n; | ||
const intPart = Math.floor(real); | ||
const decimalPart = real - intPart; | ||
fracList.forEach(function (frac, index) { | ||
var real = frac * n; | ||
var intPart = Math.floor(real); | ||
var decimalPart = real - intPart; | ||
@@ -166,11 +174,15 @@ intArray.push(intPart); | ||
// how many left ? | ||
const left = n - intArray.reduce((a, b) => a + b, 0); | ||
var left = n - intArray.reduce(function (a, b) { | ||
return a + b; | ||
}, 0); | ||
// sort O -> o | ||
decimalArray.sort((a, b) => b.value - a.value); | ||
decimalArray.sort(function (a, b) { | ||
return b.value - a.value; | ||
}); | ||
// assign the left number regard to the decimal part's value | ||
// until nothing left | ||
for (let i = 0; i < left; i++) { | ||
const targetIndex = decimalArray[i].index; | ||
for (var i = 0; i < left; i++) { | ||
var targetIndex = decimalArray[i].index; | ||
intArray[targetIndex] = intArray[targetIndex] + 1; | ||
@@ -189,3 +201,3 @@ } | ||
if (array.length < 2) { | ||
throw MIN_ARRAY_LENGTH; | ||
throw _errors.MIN_ARRAY_LENGTH; | ||
} | ||
@@ -195,3 +207,3 @@ | ||
if (array[array.length - 1].frac) { | ||
throw NO_EDN_FRAC; | ||
throw _errors.NO_EDN_FRAC; | ||
} | ||
@@ -201,3 +213,3 @@ | ||
if (n <= array.length) { | ||
throw COLOR_NUMBER_ERROR; | ||
throw _errors.COLOR_NUMBER_ERROR; | ||
} | ||
@@ -207,9 +219,11 @@ | ||
if (typeof array[0] !== 'string') { | ||
const fracSum = array.slice(0, array.length - 1).reduce((a, b) => a + b.frac, 0); | ||
var fracSum = array.slice(0, array.length - 1).reduce(function (a, b) { | ||
return a + b.frac; | ||
}, 0); | ||
if (fracSum < 0.99) { | ||
throw FRAC_SUM_ERROR; | ||
throw _errors.FRAC_SUM_ERROR; | ||
} | ||
} | ||
let result; | ||
var result = void 0; | ||
@@ -225,2 +239,2 @@ if (typeof array[0] === 'string') { | ||
module.exports = gradient; | ||
exports.default = gradient; |
{ | ||
"name": "gradient-color", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Gradient color generator that lives in 21st", | ||
@@ -10,3 +10,3 @@ "main": "./build/index.js", | ||
"scripts": { | ||
"test": "tape test/*.js | tap-notify | tap-diff", | ||
"test": "babel-tape-runner test/*.js | tap-notify | tap-diff", | ||
"dev": "babel -w ./src --out-dir ./build", | ||
@@ -31,4 +31,5 @@ "build": "babel ./src --out-dir ./build" | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-tape-runner": "^2.0.1", | ||
"blue-tape": "^1.0.0", | ||
@@ -38,4 +39,4 @@ "tape": "^4.6.3" | ||
"dependencies": { | ||
"color": "^1.0.3" | ||
"color": "^2.0.1" | ||
} | ||
} |
@@ -5,5 +5,5 @@ /** | ||
exports.NO_EDN_FRAC = new Error('The last element in the color array should not have a frac field') | ||
exports.MIN_ARRAY_LENGTH = new Error('Color array length must > 1') | ||
exports.FRAC_SUM_ERROR = new Error('The sum of the fracs should = 1') | ||
exports.COLOR_NUMBER_ERROR = new Error('The number of generated colors should >= the number of color stops') | ||
export const NO_EDN_FRAC = new Error('The last element in the color array should not have a frac field') | ||
export const MIN_ARRAY_LENGTH = new Error('Color array length must > 1') | ||
export const FRAC_SUM_ERROR = new Error('The sum of the fracs should = 1') | ||
export const COLOR_NUMBER_ERROR = new Error('The number of generated colors should >= the number of color stops') |
@@ -1,14 +0,14 @@ | ||
const Color = require('color') | ||
const { | ||
import Color from 'color' | ||
import { | ||
NO_EDN_FRAC, | ||
MIN_ARRAY_LENGTH, | ||
FRAC_SUM_ERROR, | ||
COLOR_NUMBER_ERROR | ||
} = require('./errors') | ||
COLOR_NUMBER_ERROR, | ||
} from './errors' | ||
/** | ||
* Generate n colors with given color stops | ||
* | ||
* @param {Array} colorArray | ||
* @param {Number} n number of colors that need to generate | ||
* | ||
* @param {Array} colorArray | ||
* @param {Number} n number of colors that need to generate | ||
* @returns {Array} array of generated colors in rgb mode | ||
@@ -68,8 +68,8 @@ */ | ||
* * -> generated color | ||
* | ||
* | ||
* o * * * | o * * * * | o * * o -> generated color list in char version | ||
* 4 5 4 -> assigned number of colors need to be generated | ||
* | ||
* | ||
* The last section, the end color should be considered in the generated colors | ||
* | ||
* | ||
* @returns {Array} array of colors in Color(pkg) format, need toString() call | ||
@@ -98,3 +98,3 @@ */ | ||
const deltaB = (end.blue() - start.blue()) / num | ||
// generate num colors | ||
@@ -105,3 +105,3 @@ for (let i = 0; i < num; i++) { | ||
const B = start.blue() + i * deltaB | ||
list.push(Color.rgb(R, G, B)) | ||
@@ -114,3 +114,3 @@ } | ||
} | ||
result = result.concat(list) | ||
@@ -124,19 +124,19 @@ }) | ||
* Calculate and optimize the number of each color period | ||
* | ||
* | ||
* Sometimes frac * N might be a fraction | ||
* So we use this algorithm: | ||
* | ||
* | ||
* 1. Split the number into 2 parts, each part fits in an array: | ||
* [2, 4, 1, 5] -> int array | ||
* [0.2, 0.5, 0.9, 0.3] -> decimal array | ||
* | ||
* | ||
* The left number should be: | ||
* left = N - sum(intArray) | ||
* | ||
* | ||
* 2. Sort the decimal array from large to small, assign left to | ||
* the corresponding element in intArray one by one | ||
* until left === 0 | ||
* | ||
* | ||
* 3. There goes your final array! | ||
* | ||
* | ||
* @returns {Array} array of optimized color numbers | ||
@@ -167,3 +167,3 @@ */ | ||
decimalArray.sort((a, b) => b.value - a.value) | ||
// assign the left number regard to the decimal part's value | ||
@@ -173,3 +173,3 @@ // until nothing left | ||
const targetIndex = decimalArray[i].index | ||
intArray[targetIndex] = intArray[targetIndex] + 1 | ||
intArray[targetIndex] = intArray[targetIndex] + 1 | ||
} | ||
@@ -221,2 +221,2 @@ | ||
module.exports = gradient | ||
export default gradient |
@@ -1,3 +0,3 @@ | ||
const test = require('blue-tape') | ||
const gradient = require('../build/index') | ||
import test from 'blue-tape' | ||
import gradient from '../build/index' | ||
@@ -52,2 +52,2 @@ | ||
t.is(arr[22], 'rgb(46, 164, 168)') | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
424
0
15551
5
8
+ Addedcolor@2.0.1(transitive)
- Removedcolor@1.0.3(transitive)
Updatedcolor@^2.0.1