d3plus-color
Version:
Color functions that extent the ability of d3-color.
26 lines (25 loc) • 1.08 kB
JavaScript
import { hsl } from "d3-color";
/**
@function colorSubtract
@desc Subtracts one color from another.
@param {String} c1 The base color, a valid CSS color string.
@param {String} c2 The color to remove from the base color, also a valid CSS color string.
@param {String} [o1 = 1] Value from 0 to 1 of the first color's opacity.
@param {String} [o2 = 1] Value from 0 to 1 of the first color's opacity.
@returns {String}
*/
export default function (c1, c2) {
var o1 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var o2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
c1 = hsl(c1);
c2 = hsl(c2);
var d = c2.h * o2 - c1.h * o1;
if (Math.abs(d) > 180) d -= 360;
var h = (c1.h - d) % 360;
var l = c1.l - (c2.l * o2 - c1.l * o1) / 2,
s = c1.s - (c2.s * o2 - c1.s * o1) / 2;
// a = o1 - (o2 - o1) / 2;
if (h < 0) h += 360;
return hsl("hsl(".concat(h, ",").concat(s * 100, "%,").concat(l * 100, "%)")).toString();
// return hsl(`hsl(${h},${s * 100}%,${l * 100}%,${a})`).toString();
}