Skip to content

Commit

Permalink
replace deps with d3-color
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Mar 17, 2023
1 parent b99ac02 commit 0229ab5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 95 deletions.
91 changes: 8 additions & 83 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@
"yootils": "^0.3.1"
},
"dependencies": {
"color": "^4.2.3",
"d3-array": "^3.2.1",
"d3-color": "^3.1.0",
"d3-scale": "^4.0.2",
"d3-shape": "^3.2.0",
"is-css-color": "^1.0.0"
"d3-shape": "^3.2.0"
},
"exports": {
".": {
Expand Down
32 changes: 23 additions & 9 deletions src/lib/helpers/printDebug.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import isCSSColor from 'is-css-color';
import Color from 'color';
import { rgb } from 'd3-color'

import findScaleName from './findScaleName.js';
import t from './toTitleCase.js';

const indent = ' ';

function getRgb(clr){
const { r, g, b, opacity: o } = rgb(clr);
if (![r, g, b].every(c => c >= 0 && c <= 255)) {
return false;
}
return { r, g, b, o };
}

/**
* Calculate human-perceived lightness from RGB
* This doesn't take opacity into account
* https://stackoverflow.com/a/596243
*/
function contrast({ r, g, b }) {
const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255
return luminance > 0.6 ? 'black' : 'white';
}

/* --------------------------------------------
*
* Print out the values of an object
Expand Down Expand Up @@ -60,8 +77,9 @@ function printColorArray(colorValues, method, values) {
function colorizeArray(arr) {
const colors = [];
const a = arr.map((d, i) => {
if (isCSSColor(d)) {
colors.push(d);
const rgbo = getRgb(d);
if (rgbo !== false) {
colors.push(rgbo);
// Add a space to the last item
const space = i === arr.length - 1 ? ' ' : '';
return `%c ${d}${space}`;
Expand All @@ -72,13 +90,9 @@ function colorizeArray(arr) {
return [
`%c[ ${a.join(', ')}`,
colors.map(
d => `background-color: ${d}; color:${contrast(d)};`
d => `background-color: rgba(${d.r}, ${d.g}, ${d.b}, ${d.o}); color:${contrast(d)};`
)
];
}
return null;
}

function contrast(color) {
return Color(color).isLight() ? '#000' : '#fff';
}

0 comments on commit 0229ab5

Please sign in to comment.