From 2bdee8e5f55f2cf33513c774e71f480170afdb4e Mon Sep 17 00:00:00 2001 From: hustcc Date: Thu, 28 Sep 2023 11:23:16 +0800 Subject: [PATCH 1/2] fix: ci and update readme --- README.md | 6 + __tests__/unit/evaluators/CIEDE2000.test.ts | 2 +- .../unit/evaluators/contrastRatio.test.ts | 2 +- .../extractors/getPaletteFromImage.test.ts | 4 +- .../extractors/getPaletteFromString.test.ts | 2 +- __tests__/unit/generators/index.test.ts | 2 +- __tests__/unit/generators/random.test.ts | 2 +- __tests__/unit/optimizers/index.test.ts | 2 +- __tests__/unit/simulators/grayScale.test.ts | 2 +- __tests__/unit/utils/colorBlend.test.ts | 4 +- __tests__/unit/utils/colorComputation.test.ts | 4 +- __tests__/unit/utils/colorConversion.test.ts | 2 +- package.json | 42 +- src/extractors/getPaletteFromImage.ts | 3 +- src/extractors/quantize.js | 486 ------------------ src/generators/types.ts | 6 +- src/types.ts | 14 +- src/utils/colorComputation/colorBlend.ts | 2 +- 18 files changed, 55 insertions(+), 532 deletions(-) delete mode 100644 src/extractors/quantize.js diff --git a/README.md b/README.md index 39f2c7a..177b065 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ A JavaScript library for color computation. +[![Build Status](https://github.com/antvis/smart-color/workflows/build/badge.svg?branch=master)](https://github.com/antvis/smart-color/actions) +[![Coverage Status](https://img.shields.io/coveralls/github/antvis/smart-color/master.svg)](https://coveralls.io/github/antvis/smart-color?branch=master) +[![npm Version](https://img.shields.io/npm/v/@antv/smart-color.svg)](https://www.npmjs.com/package/@antv/smart-color) +[![npm Download](https://img.shields.io/npm/dm/@antv/smart-color.svg)](https://www.npmjs.com/package/@antv/smart-color) +[![npm License](https://img.shields.io/npm/l/@antv/smart-color.svg)](https://www.npmjs.com/package/@antv/smart-color) + ## ✨ Features * **Palette Generation**: Generate categorical or discrete scale palette based on [color scheme](https://github.com/neoddish/color-palette-json-schema#colorschemetype). diff --git a/__tests__/unit/evaluators/CIEDE2000.test.ts b/__tests__/unit/evaluators/CIEDE2000.test.ts index f3091f4..a3af0b8 100644 --- a/__tests__/unit/evaluators/CIEDE2000.test.ts +++ b/__tests__/unit/evaluators/CIEDE2000.test.ts @@ -1,5 +1,5 @@ import { Color } from '@antv/color-schema'; -import { colorDifference } from '@src/index'; +import { colorDifference } from '../../../src/index'; describe('CIEDE2000', () => { const color1: Color = { diff --git a/__tests__/unit/evaluators/contrastRatio.test.ts b/__tests__/unit/evaluators/contrastRatio.test.ts index 952dbef..2377d83 100644 --- a/__tests__/unit/evaluators/contrastRatio.test.ts +++ b/__tests__/unit/evaluators/contrastRatio.test.ts @@ -1,5 +1,5 @@ import { Color } from '@antv/color-schema'; -import { colorDifference } from '@src/index'; +import { colorDifference } from '../../../src/index'; describe('contrast ratio', () => { const color1: Color = { diff --git a/__tests__/unit/extractors/getPaletteFromImage.test.ts b/__tests__/unit/extractors/getPaletteFromImage.test.ts index b71dfde..7ffd8e6 100644 --- a/__tests__/unit/extractors/getPaletteFromImage.test.ts +++ b/__tests__/unit/extractors/getPaletteFromImage.test.ts @@ -1,5 +1,5 @@ import EventEmitter from 'wolfy87-eventemitter'; -import { getPaletteFromImage } from '@src/index'; +import { getPaletteFromImage } from '../../../src/index'; describe('import palette by image', () => { beforeAll(() => { @@ -21,7 +21,7 @@ describe('import palette by image', () => { } } - addEventListener(event, callback) { + addEventListener(event: string, callback: Function) { this.ee.addListener(event, callback); } diff --git a/__tests__/unit/extractors/getPaletteFromString.test.ts b/__tests__/unit/extractors/getPaletteFromString.test.ts index 0911919..775baaf 100644 --- a/__tests__/unit/extractors/getPaletteFromString.test.ts +++ b/__tests__/unit/extractors/getPaletteFromString.test.ts @@ -1,4 +1,4 @@ -import { getPaletteFromString } from '@src/index'; +import { getPaletteFromString } from '../../../src/index'; describe('import palette', () => { test('import palette by string', () => { diff --git a/__tests__/unit/generators/index.test.ts b/__tests__/unit/generators/index.test.ts index 34d2b6f..b27aec1 100644 --- a/__tests__/unit/generators/index.test.ts +++ b/__tests__/unit/generators/index.test.ts @@ -1,5 +1,5 @@ import { Color } from '@antv/color-schema'; -import { paletteGeneration, colorToArray, colorToHex } from '@src/index'; +import { paletteGeneration, colorToArray, colorToHex } from '../../../src/index'; describe('Palette Generator', () => { const color: Color = { model: 'rgb', value: { r: 123, g: 123, b: 0 } }; diff --git a/__tests__/unit/generators/random.test.ts b/__tests__/unit/generators/random.test.ts index f730012..d572a98 100644 --- a/__tests__/unit/generators/random.test.ts +++ b/__tests__/unit/generators/random.test.ts @@ -1,5 +1,5 @@ import { RGBColor } from '@antv/color-schema'; -import { randomColor } from '@src/generators/random'; +import { randomColor } from '../../../src/generators/random'; describe('Random Generator', () => { test('should generate random colors', () => { diff --git a/__tests__/unit/optimizers/index.test.ts b/__tests__/unit/optimizers/index.test.ts index 1d392c6..9dc4659 100644 --- a/__tests__/unit/optimizers/index.test.ts +++ b/__tests__/unit/optimizers/index.test.ts @@ -1,5 +1,5 @@ import { CategoricalPalette } from '@antv/color-schema'; -import { paletteOptimization, colorDifference, SimulationType, colorSimulation } from '@src/index'; +import { paletteOptimization, colorDifference, SimulationType, colorSimulation } from '../../../src/index'; const getMinDifference = (palette: CategoricalPalette, simulationType: SimulationType = 'normal') => { let minDifference = Infinity; diff --git a/__tests__/unit/simulators/grayScale.test.ts b/__tests__/unit/simulators/grayScale.test.ts index 52e6b02..6eded90 100644 --- a/__tests__/unit/simulators/grayScale.test.ts +++ b/__tests__/unit/simulators/grayScale.test.ts @@ -1,5 +1,5 @@ import { Color, RGBAColor } from '@antv/color-schema'; -import { colorToGray, invertGrayscale, colorSimulation } from '@src/index'; +import { colorToGray, invertGrayscale, colorSimulation } from '../../../src/index'; describe('grayscale', () => { const rgb: Color = { diff --git a/__tests__/unit/utils/colorBlend.test.ts b/__tests__/unit/utils/colorBlend.test.ts index d743da1..3ad08da 100644 --- a/__tests__/unit/utils/colorBlend.test.ts +++ b/__tests__/unit/utils/colorBlend.test.ts @@ -1,6 +1,6 @@ import { Color } from '@antv/color-schema'; -import { colorBlend, colorToHex, hexToColor } from '@src/index'; -import { WHITE } from '@src/constant'; +import { colorBlend, colorToHex, hexToColor } from '../../../src/index'; +import { WHITE } from '../../../src/constant'; const colorTop: Color = { model: 'rgb', value: { r: 91, g: 143, b: 249 } }; const colorBottom: Color = { model: 'rgb', value: { r: 97, g: 221, b: 170 } }; diff --git a/__tests__/unit/utils/colorComputation.test.ts b/__tests__/unit/utils/colorComputation.test.ts index 2bb6004..2900ad8 100644 --- a/__tests__/unit/utils/colorComputation.test.ts +++ b/__tests__/unit/utils/colorComputation.test.ts @@ -1,5 +1,5 @@ -import { hueOffset } from '@src/utils/colorComputation'; -import { hexToColor, colorToHex, colorDarken, colorBrighten } from '@src/index'; +import { hueOffset } from '../../../src/utils/colorComputation'; +import { hexToColor, colorToHex, colorDarken, colorBrighten } from '../../../src/index'; describe('Color computation', () => { test('hue offset', () => { diff --git a/__tests__/unit/utils/colorConversion.test.ts b/__tests__/unit/utils/colorConversion.test.ts index 2b1b6cc..ba6aa6f 100644 --- a/__tests__/unit/utils/colorConversion.test.ts +++ b/__tests__/unit/utils/colorConversion.test.ts @@ -1,5 +1,5 @@ import { Color } from '@antv/color-schema'; -import { arrayToColor, colorToArray, colorToGray, hexToColor, colorToHex, nameToColor } from '@src/index'; +import { arrayToColor, colorToArray, colorToGray, hexToColor, colorToHex, nameToColor } from '../../../src/index'; const black: Color = { model: 'rgb', diff --git a/package.json b/package.json index 05b0012..2e328f9 100644 --- a/package.json +++ b/package.json @@ -35,22 +35,12 @@ "ava", "color" ], - "repository": { - "type": "git", - "url": "git+https://github.com/antvis/smart-color.git" - }, - "author": { - "name": "AntV", - "url": "https://antv.vision/" - }, - "license": "MIT", - "publishConfig": { - "access": "public" - }, - "bugs": { - "url": "https://github.com/antvis/smart-color/issues" + "dependencies": { + "@antv/color-schema": "^0.2.2", + "chroma-js": "^2.1.2", + "color-blind": "^0.1.1", + "quantize": "^1.0.2" }, - "homepage": "https://antvis.github.io/smart-color", "devDependencies": { "@babel/runtime": "^7.14.6", "@commitlint/cli": "^12.1.4", @@ -100,9 +90,21 @@ "limit": "24 Kb" } ], - "dependencies": { - "@antv/color-schema": "^0.2.2", - "chroma-js": "^2.1.2", - "color-blind": "^0.1.1" - } + "repository": { + "type": "git", + "url": "git+https://github.com/antvis/smart-color.git" + }, + "author": { + "name": "AntV", + "url": "https://antv.vision/" + }, + + "publishConfig": { + "access": "public" + }, + "bugs": { + "url": "https://github.com/antvis/smart-color/issues" + }, + "homepage": "https://antvis.github.io/smart-color", + "license": "MIT" } diff --git a/src/extractors/getPaletteFromImage.ts b/src/extractors/getPaletteFromImage.ts index b1b2032..e08b679 100644 --- a/src/extractors/getPaletteFromImage.ts +++ b/src/extractors/getPaletteFromImage.ts @@ -1,10 +1,11 @@ +// @ts-ignore +import quantize from 'quantize'; import { Palette } from '@antv/color-schema'; // @ts-ignore // package quantize has error, when color number >= 7, https://github.com/olivierlesnicki/quantize/issues/9 // However, the owner of the package did not merge the pr to fix the issue. // TODO: modified median cut quantization ts version import { loadImage, arrayToColor } from '../utils'; -import quantize from './quantize'; // sample pixels in image const imageToPixels = (image: HTMLImageElement, quality: number): [number, number, number][] => { diff --git a/src/extractors/quantize.js b/src/extractors/quantize.js deleted file mode 100644 index 25e1a62..0000000 --- a/src/extractors/quantize.js +++ /dev/null @@ -1,486 +0,0 @@ -/* - * quantize.js Copyright 2008 Nick Rabinowitz - * Ported to node.js by Olivier Lesnicki - * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - */ - -// fill out a couple protovis dependencies -/* - * Block below copied from Protovis: http://mbostock.github.com/protovis/ - * Copyright 2010 Stanford Visualization Group - * Licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php - */ - -var pv = { - map: function(array, f) { - var o = {}; - return f ? array.map(function(d, i) { - o.index = i; - return f.call(o, d); - }) : array.slice(); - }, - naturalOrder: function(a, b) { - return (a < b) ? -1 : ((a > b) ? 1 : 0); - }, - sum: function(array, f) { - var o = {}; - return array.reduce(f ? function(p, d, i) { - o.index = i; - return p + f.call(o, d); - } : function(p, d) { - return p + d; - }, 0); - }, - max: function(array, f) { - return Math.max.apply(null, f ? pv.map(array, f) : array); - } -} - -/** - * Basic Javascript port of the MMCQ (modified median cut quantization) - * algorithm from the Leptonica library (http://www.leptonica.com/). - * Returns a color map you can use to map original pixels to the reduced - * palette. Still a work in progress. - * - * @author Nick Rabinowitz - * @example - -// array of pixels as [R,G,B] arrays -var myPixels = [[190,197,190], [202,204,200], [207,214,210], [211,214,211], [205,207,207] - // etc - ]; -var maxColors = 4; - -var cmap = MMCQ.quantize(myPixels, maxColors); -var newPalette = cmap.palette(); -var newPixels = myPixels.map(function(p) { - return cmap.map(p); -}); - - */ -var MMCQ = (function() { - // private constants - var sigbits = 5, - rshift = 8 - sigbits, - maxIterations = 1000, - fractByPopulations = 0.75; - - // get reduced-space color index for a pixel - - function getColorIndex(r, g, b) { - return (r << (2 * sigbits)) + (g << sigbits) + b; - } - - // Simple priority queue - function PQueue(comparator) { - var contents = [], - sorted = false; - - function sort() { - contents.sort(comparator); - sorted = true; - } - - return { - push: function(o) { - contents.push(o); - sorted = false; - }, - peek: function(index) { - if (!sorted) sort(); - if (index === undefined) index = contents.length - 1; - return contents[index]; - }, - pop: function() { - if (!sorted) sort(); - return contents.pop(); - }, - size: function() { - return contents.length; - }, - map: function(f) { - return contents.map(f); - }, - debug: function() { - if (!sorted) sort(); - return contents; - } - }; - } - - // 3d color space box - function VBox(r1, r2, g1, g2, b1, b2, histo) { - var vbox = this; - vbox.r1 = r1; - vbox.r2 = r2; - vbox.g1 = g1; - vbox.g2 = g2; - vbox.b1 = b1; - vbox.b2 = b2; - vbox.histo = histo; - } - VBox.prototype = { - volume: function(force) { - var vbox = this; - if (!vbox._volume || force) { - vbox._volume = ((vbox.r2 - vbox.r1 + 1) * (vbox.g2 - vbox.g1 + 1) * (vbox.b2 - vbox.b1 + 1)); - } - return vbox._volume; - }, - count: function(force) { - var vbox = this, - histo = vbox.histo; - if (!vbox._count_set || force) { - var npix = 0, - i, j, k, index; - for (i = vbox.r1; i <= vbox.r2; i++) { - for (j = vbox.g1; j <= vbox.g2; j++) { - for (k = vbox.b1; k <= vbox.b2; k++) { - index = getColorIndex(i, j, k); - npix += (histo[index] || 0); - } - } - } - vbox._count = npix; - vbox._count_set = true; - } - return vbox._count; - }, - copy: function() { - var vbox = this; - return new VBox(vbox.r1, vbox.r2, vbox.g1, vbox.g2, vbox.b1, vbox.b2, vbox.histo); - }, - avg: function(force) { - var vbox = this, - histo = vbox.histo; - if (!vbox._avg || force) { - var ntot = 0, - mult = 1 << (8 - sigbits), - rsum = 0, - gsum = 0, - bsum = 0, - hval, - i, j, k, histoindex; - for (i = vbox.r1; i <= vbox.r2; i++) { - for (j = vbox.g1; j <= vbox.g2; j++) { - for (k = vbox.b1; k <= vbox.b2; k++) { - histoindex = getColorIndex(i, j, k); - hval = histo[histoindex] || 0; - ntot += hval; - rsum += (hval * (i + 0.5) * mult); - gsum += (hval * (j + 0.5) * mult); - bsum += (hval * (k + 0.5) * mult); - } - } - } - if (ntot) { - vbox._avg = [~~(rsum / ntot), ~~ (gsum / ntot), ~~ (bsum / ntot)]; - } else { - //console.log('empty box'); - vbox._avg = [~~(mult * (vbox.r1 + vbox.r2 + 1) / 2), ~~ (mult * (vbox.g1 + vbox.g2 + 1) / 2), ~~ (mult * (vbox.b1 + vbox.b2 + 1) / 2)]; - } - } - return vbox._avg; - }, - contains: function(pixel) { - var vbox = this, - rval = pixel[0] >> rshift; - gval = pixel[1] >> rshift; - bval = pixel[2] >> rshift; - return (rval >= vbox.r1 && rval <= vbox.r2 && - gval >= vbox.g1 && gval <= vbox.g2 && - bval >= vbox.b1 && bval <= vbox.b2); - } - }; - - // Color map - function CMap() { - this.vboxes = new PQueue(function(a, b) { - return pv.naturalOrder( - a.vbox.count() * a.vbox.volume(), - b.vbox.count() * b.vbox.volume() - ) - });; - } - CMap.prototype = { - push: function(vbox) { - this.vboxes.push({ - vbox: vbox, - color: vbox.avg() - }); - }, - palette: function() { - return this.vboxes.map(function(vb) { - return vb.color - }); - }, - size: function() { - return this.vboxes.size(); - }, - map: function(color) { - var vboxes = this.vboxes; - for (var i = 0; i < vboxes.size(); i++) { - if (vboxes.peek(i).vbox.contains(color)) { - return vboxes.peek(i).color; - } - } - return this.nearest(color); - }, - nearest: function(color) { - var vboxes = this.vboxes, - d1, d2, pColor; - for (var i = 0; i < vboxes.size(); i++) { - d2 = Math.sqrt( - Math.pow(color[0] - vboxes.peek(i).color[0], 2) + - Math.pow(color[1] - vboxes.peek(i).color[1], 2) + - Math.pow(color[2] - vboxes.peek(i).color[2], 2) - ); - if (d2 < d1 || d1 === undefined) { - d1 = d2; - pColor = vboxes.peek(i).color; - } - } - return pColor; - }, - forcebw: function() { - // XXX: won't work yet - var vboxes = this.vboxes; - vboxes.sort(function(a, b) { - return pv.naturalOrder(pv.sum(a.color), pv.sum(b.color)) - }); - - // force darkest color to black if everything < 5 - var lowest = vboxes[0].color; - if (lowest[0] < 5 && lowest[1] < 5 && lowest[2] < 5) - vboxes[0].color = [0, 0, 0]; - - // force lightest color to white if everything > 251 - var idx = vboxes.length - 1, - highest = vboxes[idx].color; - if (highest[0] > 251 && highest[1] > 251 && highest[2] > 251) - vboxes[idx].color = [255, 255, 255]; - } - }; - - // histo (1-d array, giving the number of pixels in - // each quantized region of color space), or null on error - - function getHisto(pixels) { - var histosize = 1 << (3 * sigbits), - histo = new Array(histosize), - index, rval, gval, bval; - pixels.forEach(function(pixel) { - rval = pixel[0] >> rshift; - gval = pixel[1] >> rshift; - bval = pixel[2] >> rshift; - index = getColorIndex(rval, gval, bval); - histo[index] = (histo[index] || 0) + 1; - }); - return histo; - } - - function vboxFromPixels(pixels, histo) { - var rmin = 1000000, - rmax = 0, - gmin = 1000000, - gmax = 0, - bmin = 1000000, - bmax = 0, - rval, gval, bval; - // find min/max - pixels.forEach(function(pixel) { - rval = pixel[0] >> rshift; - gval = pixel[1] >> rshift; - bval = pixel[2] >> rshift; - if (rval < rmin) rmin = rval; - else if (rval > rmax) rmax = rval; - if (gval < gmin) gmin = gval; - else if (gval > gmax) gmax = gval; - if (bval < bmin) bmin = bval; - else if (bval > bmax) bmax = bval; - }); - return new VBox(rmin, rmax, gmin, gmax, bmin, bmax, histo); - } - - function medianCutApply(histo, vbox) { - if (!vbox.count()) return; - - var rw = vbox.r2 - vbox.r1 + 1, - gw = vbox.g2 - vbox.g1 + 1, - bw = vbox.b2 - vbox.b1 + 1, - maxw = pv.max([rw, gw, bw]); - // only one pixel, no split - if (vbox.count() == 1) { - return [vbox.copy()] - } - /* Find the partial sum arrays along the selected axis. */ - var total = 0, - partialsum = [], - lookaheadsum = [], - i, j, k, sum, index; - if (maxw == rw) { - for (i = vbox.r1; i <= vbox.r2; i++) { - sum = 0; - for (j = vbox.g1; j <= vbox.g2; j++) { - for (k = vbox.b1; k <= vbox.b2; k++) { - index = getColorIndex(i, j, k); - sum += (histo[index] || 0); - } - } - total += sum; - partialsum[i] = total; - } - } else if (maxw == gw) { - for (i = vbox.g1; i <= vbox.g2; i++) { - sum = 0; - for (j = vbox.r1; j <= vbox.r2; j++) { - for (k = vbox.b1; k <= vbox.b2; k++) { - index = getColorIndex(j, i, k); - sum += (histo[index] || 0); - } - } - total += sum; - partialsum[i] = total; - } - } else { /* maxw == bw */ - for (i = vbox.b1; i <= vbox.b2; i++) { - sum = 0; - for (j = vbox.r1; j <= vbox.r2; j++) { - for (k = vbox.g1; k <= vbox.g2; k++) { - index = getColorIndex(j, k, i); - sum += (histo[index] || 0); - } - } - total += sum; - partialsum[i] = total; - } - } - partialsum.forEach(function(d, i) { - lookaheadsum[i] = total - d - }); - - function doCut(color) { - var dim1 = color + '1', - dim2 = color + '2', - left, right, vbox1, vbox2, d2, count2 = 0; - for (i = vbox[dim1]; i <= vbox[dim2]; i++) { - if (partialsum[i] > total / 2) { - vbox1 = vbox.copy(); - vbox2 = vbox.copy(); - left = i - vbox[dim1]; - right = vbox[dim2] - i; - if (left <= right) - d2 = Math.min(vbox[dim2] - 1, ~~ (i + right / 2)); - else d2 = Math.max(vbox[dim1], ~~ (i - 1 - left / 2)); - // avoid 0-count boxes - while (!partialsum[d2]) d2++; - count2 = lookaheadsum[d2]; - while (!count2 && partialsum[d2 - 1]) count2 = lookaheadsum[--d2]; - // set dimensions - vbox1[dim2] = d2; - vbox2[dim1] = vbox1[dim2] + 1; - // console.log('vbox counts:', vbox.count(), vbox1.count(), vbox2.count()); - return [vbox1, vbox2]; - } - } - - } - // determine the cut planes - return maxw == rw ? doCut('r') : - maxw == gw ? doCut('g') : - doCut('b'); - } - - function quantize(pixels, maxcolors) { - // short-circuit - if (!pixels.length || maxcolors < 2 || maxcolors > 256) { - // console.log('wrong number of maxcolors'); - return false; - } - - // XXX: check color content and convert to grayscale if insufficient - - var histo = getHisto(pixels), - histosize = 1 << (3 * sigbits); - - // check that we aren't below maxcolors already - var nColors = 0; - histo.forEach(function() { - nColors++ - }); - if (nColors <= maxcolors) { - // XXX: generate the new colors from the histo and return - } - - // get the beginning vbox from the colors - var vbox = vboxFromPixels(pixels, histo), - pq = new PQueue(function(a, b) { - return pv.naturalOrder(a.count(), b.count()) - }); - pq.push(vbox); - - // inner function to do the iteration - - function iter(lh, target) { - var ncolors = lh.size(), - niters = 0, - vbox; - while (niters < maxIterations) { - if (ncolors >= target) return; - if (niters++ > maxIterations) { - // console.log("infinite loop; perhaps too few pixels!"); - return; - } - vbox = lh.pop(); - if (!vbox.count()) { /* just put it back */ - lh.push(vbox); - niters++; - continue; - } - // do the cut - var vboxes = medianCutApply(histo, vbox), - vbox1 = vboxes[0], - vbox2 = vboxes[1]; - - if (!vbox1) { - // console.log("vbox1 not defined; shouldn't happen!"); - return; - } - lh.push(vbox1); - if (vbox2) { /* vbox2 can be null */ - lh.push(vbox2); - ncolors++; - } - } - } - - // first set of colors, sorted by population - iter(pq, fractByPopulations * maxcolors); - // console.log(pq.size(), pq.debug().length, pq.debug().slice()); - - // Re-sort by the product of pixel occupancy times the size in color space. - var pq2 = new PQueue(function(a, b) { - return pv.naturalOrder(a.count() * a.volume(), b.count() * b.volume()) - }); - while (pq.size()) { - pq2.push(pq.pop()); - } - - // next set - generate the median cuts using the (npix * vol) sorting. - iter(pq2, maxcolors); - - // calculate the actual colors - var cmap = new CMap(); - while (pq2.size()) { - cmap.push(pq2.pop()); - } - - return cmap; - } - - return { - quantize: quantize - } -})(); - -export default MMCQ.quantize \ No newline at end of file diff --git a/src/generators/types.ts b/src/generators/types.ts index d7e8d52..653dfff 100644 --- a/src/generators/types.ts +++ b/src/generators/types.ts @@ -1,6 +1,6 @@ import { Palette } from '@antv/color-schema'; import { GeneratorConfiguration } from '../types'; -export type Generation = ( - configuration: { [K in keyof GeneratorConfiguration]-?: GeneratorConfiguration[K] } -) => Palette; +export type Generation = (configuration: { + [K in keyof GeneratorConfiguration]-?: GeneratorConfiguration[K]; +}) => Palette; diff --git a/src/types.ts b/src/types.ts index 587c237..51d5938 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,7 +17,7 @@ import { } from './constant'; // color simulation -export type ColorBlindnessSimulationType = typeof COLOR_BLINDNESS_SIMULATION_TYPES[number]; +export type ColorBlindnessSimulationType = (typeof COLOR_BLINDNESS_SIMULATION_TYPES)[number]; export type GrayscaleType = 'grayscale'; export type SimulationType = ColorBlindnessSimulationType | GrayscaleType; export type ColorSimulation = ( @@ -27,7 +27,7 @@ export type ColorSimulation = ( ) => Color; // professional test -export type ColorDifferenceMeasure = typeof COLOR_DIFFERENCE_MEASURES[number]; +export type ColorDifferenceMeasure = (typeof COLOR_DIFFERENCE_MEASURES)[number]; type ColorDifferenceGeneralConfiguration = { // If the color is semi-transparent, the color will be overlapped on the backgroundColor // default value: white @@ -45,7 +45,7 @@ export type ColorDifferenceConfiguration = ColorDifferenceGeneralConfiguration & } ); export type ColorDifference = (color1: Color, color2: Color, configuration?: ColorDifferenceConfiguration) => number; -export type ColorAestheticMeasure = typeof COLOR_AESTHETIC_MEASURES[number]; +export type ColorAestheticMeasure = (typeof COLOR_AESTHETIC_MEASURES)[number]; export type ColorAestheticConfiguration = { measure: ColorAestheticMeasure; // If the color is semi-transparent, the color will be overlapped on the backgroundColor @@ -71,7 +71,7 @@ export type OptimizerConfiguration = { export type PaletteOptimization = (palette: Palette, configuration?: OptimizerConfiguration) => Palette; // palette generation -type Tendency = typeof TENDENCIES[number]; +type Tendency = (typeof TENDENCIES)[number]; export type GeneratorConfiguration = { count?: number; // basic color @@ -88,9 +88,9 @@ export type PaletteGeneration = ( ) => CategoricalPalette | DiscreteScalePalette; // color compution -export type SeparableBlendMode = typeof SEPARABLE_BLEND_MODES[number]; -export type NonSeparableBlendMode = typeof NON_SEPARABLE_BLEND_MODES[number]; -export type BlendMode = typeof BLEND_MODES[number]; +export type SeparableBlendMode = (typeof SEPARABLE_BLEND_MODES)[number]; +export type NonSeparableBlendMode = (typeof NON_SEPARABLE_BLEND_MODES)[number]; +export type BlendMode = (typeof BLEND_MODES)[number]; export type ColorBlend = (colorTop: Color, colorBottom: Color, mode?: BlendMode) => Color; export type ColorDarken = (color: Color, value?: number) => Color; export type ColorBrighten = (color: Color, value?: number) => Color; diff --git a/src/utils/colorComputation/colorBlend.ts b/src/utils/colorComputation/colorBlend.ts index fc860f0..c76dcb3 100644 --- a/src/utils/colorComputation/colorBlend.ts +++ b/src/utils/colorComputation/colorBlend.ts @@ -1,6 +1,6 @@ import { NON_SEPARABLE_BLEND_MODES, SEPARABLE_BLEND_MODES } from '../../constant'; import { SeparableBlendMode, NonSeparableBlendMode, ColorBlend } from '../../types'; -import { includes } from '..'; +import { includes } from '../helper'; import { colorToArray } from '../colorConversion'; // ref: [wiki] https://en.wikipedia.org/wiki/Blend_modes From 3b29ccf6bfed7249e4fafc6fa81f201f0282726f Mon Sep 17 00:00:00 2001 From: hustcc Date: Thu, 28 Sep 2023 11:27:58 +0800 Subject: [PATCH 2/2] chore: open ci --- .github/workflows/.after-public.go-build | 37 ------------------------ .github/workflows/build.yml | 37 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 .github/workflows/.after-public.go-build create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/.after-public.go-build b/.github/workflows/.after-public.go-build deleted file mode 100644 index 40b31cd..0000000 --- a/.github/workflows/.after-public.go-build +++ /dev/null @@ -1,37 +0,0 @@ -# name: build - -# on: [push, pull_request] - -# jobs: -# build: -# runs-on: ubuntu-latest - -# steps: -# - name: Checkout -# uses: actions/checkout@v2.3.4 - -# - name: Setup Node.js environment -# uses: actions/setup-node@v2.1.5 -# with: -# node-version: '12' - -# - name: Cache node modules -# uses: actions/cache@v2 -# env: -# cache-name: cache-node-modules -# with: -# path: ./node_modules -# key: ${{ runner.os }}-build-cache-node-modules-${{ hashFiles('**/package.json') }} -# restore-keys: | -# cache-node-modules- - -# - name: Run ci -# run: | -# npm install -# npm run ci - -# - name: coverall -# if: success() -# uses: coverallsapp/github-action@master -# with: -# github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..c9ae68a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,37 @@ +name: build + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2.3.4 + + - name: Setup Node.js environment + uses: actions/setup-node@v2.1.5 + with: + node-version: '12' + + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + path: ./node_modules + key: ${{ runner.os }}-build-cache-node-modules-${{ hashFiles('**/package.json') }} + restore-keys: | + cache-node-modules- + + - name: Run ci + run: | + npm install + npm run ci + + - name: coverall + if: success() + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }}