class ColorSurface {
constructor(fnSurface, space) {
this.fnSurface = fnSurface;
this.space = space;
}
get raster100() {
const fnSurface = this.fnSurface;
const toSrgb = _.flow([
colorUtils.fromPolar,
this.space.from100,
this.space.toSRgb
]);
return function (chroma, luminance) {
try {
const hue = fnSurface(luminance);
const sRgb = toSrgb([luminance, chroma, hue]);
const hex = colorUtils.inRgbGamut(sRgb)
? colorUtils.hexFromSRgb(sRgb)
: undefined;
return hex;
} catch (error) {
throw `${error} for {luminance: ${luminance}, chroma: ${chroma}}`;
}
};
}
plotMarks100 = ({ negateChroma = false } = {}) => {
return [
Plot.raster({
fill: negateChroma ? (x, y) => this.raster100(-x, y) : this.raster100,
x1: 0,
x2: negateChroma
? -this.space.scale.maxChroma100
: this.space.scale.maxChroma100,
y1: 0,
y2: 100
})
];
};
// todo: serialize to object, create from object
static fromHue = function (
hue,
{ lum100 = 50, dHue100 = 0, space = colorSpaces.munsell } = {}
) {
if (!(typeof hue === "number")) {
throw "hue is not a number";
}
const fnSurface = function (luminance) {
return hue + dHue100 * (luminance - lum100);
};
return new ColorSurface(fnSurface, space);
};
static fromHex = function (
hex,
{ dHue100 = 0, space = colorSpaces.munsell } = {}
) {
if (!(typeof hex === "string")) {
throw "hex is not a string";
}
const [lum100, , hue] = _.flow(
colorUtils.hexToSRgb,
space.fromSRgb,
space.to100,
colorUtils.toPolar
)(hex);
return this.fromHue(hue, {
lum100: lum100,
dHue100: dHue100,
space: space
});
};
static fromHexHex = function (
hex1,
hex2,
{ short = true, space = colorSpaces.munsell } = {}
) {
if (!(typeof hex1 === "string")) {
throw "hex1 is not a string";
}
if (!(typeof hex2 === "string")) {
throw "hex2 is not a string";
}
const toPolar = _.flow(
colorUtils.hexToSRgb,
space.fromSRgb,
space.to100,
colorUtils.toPolar
);
const [lum100_1, , hue1] = toPolar(hex1);
const [lum100_2, , hue2] = toPolar(hex2);
const dHue100 =
deltaHue(hue1, hue2, { short: short }) / (lum100_2 - lum100_1);
return this.fromHue(hue1, {
lum100: lum100_1,
dHue100: dHue100,
space: space
});
};
static fromHexArray(arrHex, { space = colorSpaces.jzazbz }) {
// idea: spline hue = f(luminance)
// need to assure monotonicity
throw "not implemented";
}
}