Skip to content

Commit

Permalink
prettier code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Jul 28, 2024
1 parent af9841d commit eefb776
Show file tree
Hide file tree
Showing 92 changed files with 1,028 additions and 815 deletions.
13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"docs-preview": "cd docs && make && make preview",
"test": "vitest test/*.js",
"test:old": "vows --dot-matrix test/*.cjs",
"lint": "eslint index.js index-light.js src"
"lint": "eslint index.js index-light.js src",
"format": "prettier --write index.js index-light.js src *.config.js"
},
"devDependencies": {
"@babel/eslint-parser": "^7.24.7",
Expand All @@ -42,6 +43,7 @@
"husky": "^9.0.11",
"markdown-to-html": "0.0.13",
"minimatch": "^10.0.1",
"prettier": "^3.3.3",
"rollup": "^4.18.1",
"rollup-plugin-license": "^3.5.2",
"vitest": "^2.0.4",
Expand All @@ -56,15 +58,6 @@
"test"
]
},
"prettier": {
"arrowParens": "avoid",
"printWidth": 100,
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "none",
"svelteSortOrder": "options-scripts-styles-markup"
},
"husky": {
"hooks": {
"pre-commit": "npm run lint",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// prettier.config.js, .prettierrc.js, prettier.config.mjs, or .prettierrc.mjs

/**
* @see https://prettier.io/docs/en/configuration.html
* @type {import("prettier").Config}
*/
const config = {
trailingComma: 'none',
tabWidth: 4,
semi: true,
singleQuote: true,
printWidth: 80,
overrides: [
{
files: 'src/colors/colorbrewer.js',
options: {
printWidth: 160
}
}
]
};

export default config;
80 changes: 40 additions & 40 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import buble from "@rollup/plugin-buble";
import license from "rollup-plugin-license";
import replace from "@rollup/plugin-replace";
import path from "path";
import terser from "@rollup/plugin-terser";
import buble from '@rollup/plugin-buble';
import license from 'rollup-plugin-license';
import replace from '@rollup/plugin-replace';
import path from 'path';
import terser from '@rollup/plugin-terser';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

Expand All @@ -13,44 +13,44 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default [
bundle("index.js", "chroma"),
bundle("index-light.js", "chroma-light"),
bundle('index.js', 'chroma'),
bundle('index-light.js', 'chroma-light')
];

function bundle(input, target) {
return {
input,
output: {
file: `${target}${minify ? ".min" : ""}.cjs`,
format: "umd",
name: "chroma",
},
plugins: [
replace({
delimiters: ["@@", ""],
preventAssignment: true,
version: '0.4.2'
}),
return {
input,
output: {
file: `${target}${minify ? '.min' : ''}.cjs`,
format: 'umd',
name: 'chroma'
},
plugins: [
replace({
delimiters: ['@@', ''],
preventAssignment: true,
version: '0.4.2'
}),

// If we're building for production (npm run build
// instead of npm run dev), transpile and minify
buble({
transforms: { dangerousForOf: true },
}),
minify &&
terser({
mangle: true,
}),
license({
sourcemap: true,
//cwd: '.', // Default is process.cwd()
// If we're building for production (npm run build
// instead of npm run dev), transpile and minify
buble({
transforms: { dangerousForOf: true }
}),
minify &&
terser({
mangle: true
}),
license({
sourcemap: true,
//cwd: '.', // Default is process.cwd()

banner: {
content: {
file: path.join(__dirname, "LICENSE"),
},
},
}),
],
};
banner: {
content: {
file: path.join(__dirname, 'LICENSE')
}
}
})
]
};
}
11 changes: 8 additions & 3 deletions src/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import _input from './io/input';
class Color {
constructor(...args) {
const me = this;
if (type(args[0]) === 'object' &&
if (
type(args[0]) === 'object' &&
args[0].constructor &&
args[0].constructor === this.constructor) {
args[0].constructor === this.constructor
) {
// the argument is already a Color instance
return args[0];
}
Expand All @@ -26,7 +28,10 @@ class Color {
}
}
if (_input.format[mode]) {
const rgb = _input.format[mode].apply(null, autodetect ? args : args.slice(0, -1));
const rgb = _input.format[mode].apply(
null,
autodetect ? args : args.slice(0, -1)
);
me._rgb = clip_rgb(rgb);
} else {
throw new Error('unknown format: ' + args);
Expand Down
4 changes: 2 additions & 2 deletions src/chroma.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Color from './Color';

const chroma = (...args) => {
return new chroma.Color(...args);
return new chroma.Color(...args);
};

chroma.Color = Color;
chroma.version = '@@version'
chroma.version = '@@version';

export default chroma;
6 changes: 2 additions & 4 deletions src/colors/colorbrewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const colorbrewer = {
Viridis: ['#440154', '#482777', '#3f4a8a', '#31678e', '#26838f', '#1f9d8a', '#6cce5a', '#b6de2b', '#fee825'],

// diverging

Spectral: ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2'],
RdYlGn: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850', '#006837'],
RdBu: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#f7f7f7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'],
Expand All @@ -50,16 +49,15 @@ const colorbrewer = {
PuOr: ['#7f3b08', '#b35806', '#e08214', '#fdb863', '#fee0b6', '#f7f7f7', '#d8daeb', '#b2abd2', '#8073ac', '#542788', '#2d004b'],

// qualitative

Set2: ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f', '#e5c494', '#b3b3b3'],
Accent: ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666'],
Set1: ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'],
Set3: ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f'],
Dark2: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666'],
Paired: ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'],
Pastel2: ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae', '#f1e2cc', '#cccccc'],
Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2'],
}
Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']
};

// add lowercase aliases for case-insensitive matches
for (let key of Object.keys(colorbrewer)) {
Expand Down
2 changes: 1 addition & 1 deletion src/colors/w3cx11.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,6 @@ const w3cx11 = {
whitesmoke: '#f5f5f5',
yellow: '#ffff00',
yellowgreen: '#9acd32'
}
};

export default w3cx11;
57 changes: 31 additions & 26 deletions src/generator/average.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,86 @@ import { clip_rgb } from '../utils/index.js';

const { pow, sqrt, PI, cos, sin, atan2 } = Math;

export default (colors, mode='lrgb', weights=null) => {
export default (colors, mode = 'lrgb', weights = null) => {
const l = colors.length;
if (!weights) weights = Array.from(new Array(l)).map(() => 1);
// normalize weights
const k = l / weights.reduce(function(a, b) { return a + b; });
weights.forEach((w,i) => { weights[i] *= k })
const k =
l /
weights.reduce(function (a, b) {
return a + b;
});
weights.forEach((w, i) => {
weights[i] *= k;
});
// convert colors to Color objects
colors = colors.map(c => new Color(c));
colors = colors.map((c) => new Color(c));
if (mode === 'lrgb') {
return _average_lrgb(colors, weights)
return _average_lrgb(colors, weights);
}
const first = colors.shift();
const xyz = first.get(mode);
const cnt = [];
let dx = 0;
let dy = 0;
// initial color
for (let i=0; i<xyz.length; i++) {
for (let i = 0; i < xyz.length; i++) {
xyz[i] = (xyz[i] || 0) * weights[0];
cnt.push(isNaN(xyz[i]) ? 0 : weights[0]);
if (mode.charAt(i) === 'h' && !isNaN(xyz[i])) {
const A = xyz[i] / 180 * PI;
const A = (xyz[i] / 180) * PI;
dx += cos(A) * weights[0];
dy += sin(A) * weights[0];
}
}

let alpha = first.alpha() * weights[0];
colors.forEach((c,ci) => {
colors.forEach((c, ci) => {
const xyz2 = c.get(mode);
alpha += c.alpha() * weights[ci+1];
for (let i=0; i<xyz.length; i++) {
alpha += c.alpha() * weights[ci + 1];
for (let i = 0; i < xyz.length; i++) {
if (!isNaN(xyz2[i])) {
cnt[i] += weights[ci+1];
cnt[i] += weights[ci + 1];
if (mode.charAt(i) === 'h') {
const A = xyz2[i] / 180 * PI;
dx += cos(A) * weights[ci+1];
dy += sin(A) * weights[ci+1];
const A = (xyz2[i] / 180) * PI;
dx += cos(A) * weights[ci + 1];
dy += sin(A) * weights[ci + 1];
} else {
xyz[i] += xyz2[i] * weights[ci+1];
xyz[i] += xyz2[i] * weights[ci + 1];
}
}
}
});

for (let i=0; i<xyz.length; i++) {
for (let i = 0; i < xyz.length; i++) {
if (mode.charAt(i) === 'h') {
let A = atan2(dy / cnt[i], dx / cnt[i]) / PI * 180;
let A = (atan2(dy / cnt[i], dx / cnt[i]) / PI) * 180;
while (A < 0) A += 360;
while (A >= 360) A -= 360;
xyz[i] = A;
} else {
xyz[i] = xyz[i]/cnt[i];
xyz[i] = xyz[i] / cnt[i];
}
}
alpha /= l;
return (new Color(xyz, mode)).alpha(alpha > 0.99999 ? 1 : alpha, true);
return new Color(xyz, mode).alpha(alpha > 0.99999 ? 1 : alpha, true);
};


const _average_lrgb = (colors, weights) => {
const l = colors.length;
const xyz = [0,0,0,0];
for (let i=0; i < colors.length; i++) {
const xyz = [0, 0, 0, 0];
for (let i = 0; i < colors.length; i++) {
const col = colors[i];
const f = weights[i] / l;
const rgb = col._rgb;
xyz[0] += pow(rgb[0],2) * f;
xyz[1] += pow(rgb[1],2) * f;
xyz[2] += pow(rgb[2],2) * f;
xyz[0] += pow(rgb[0], 2) * f;
xyz[1] += pow(rgb[1], 2) * f;
xyz[2] += pow(rgb[2], 2) * f;
xyz[3] += rgb[3] * f;
}
xyz[0] = sqrt(xyz[0]);
xyz[1] = sqrt(xyz[1]);
xyz[2] = sqrt(xyz[2]);
if (xyz[3] > 0.9999999) xyz[3] = 1;
return new Color(clip_rgb(xyz));
}
};
Loading

0 comments on commit eefb776

Please sign in to comment.