whee 2.4 gamma

This commit is contained in:
Brooke Vibber 2022-11-05 19:38:31 -07:00
parent 16c2caba95
commit e3e1dd7c6e

View file

@ -1,20 +1,15 @@
function sRGBToLinear(val) { function toLinear(val) {
// use a 2.4 gamma approximation
// this is BT.1886 compatible
// and simpler than sRGB
let unit = val / 255; let unit = val / 255;
if (unit < 0.04045) { unit **= 2.4;
unit /= 12.92;
} else {
unit = ((unit + 0.055) / 1.055) ** 2.4;
}
return unit * 255; return unit * 255;
} }
function linearTosRGB(val) { function fromLinear(val) {
let unit = val / 255; let unit = val / 255;
if (unit < 0.0031308) { unit **= (1 / 2.4);
unit *= 12.92;
} else {
unit = 1.055 * unit ** (1 / 2.4) - 0.055;
}
return unit * 255; return unit * 255;
} }
@ -32,19 +27,19 @@ class RGB {
return new RGB(r,g,b); return new RGB(r,g,b);
} }
sRGBToLinear() { toLinear() {
return new RGB( return new RGB(
sRGBToLinear(this.r), toLinear(this.r),
sRGBToLinear(this.g), toLinear(this.g),
sRGBToLinear(this.b) toLinear(this.b)
); );
} }
linearTosRGB() { fromLinear() {
return new RGB( return new RGB(
linearTosRGB(this.r), fromLinear(this.r),
linearTosRGB(this.g), fromLinear(this.g),
linearTosRGB(this.b) fromLinear(this.b)
); );
} }
@ -357,7 +352,7 @@ let palette256 = [
0xf6e46f, 0xf6e46f,
0xfffa84, 0xfffa84,
0xffff99, 0xffff99,
].map((hex) => RGB.fromHex(hex).sRGBToLinear()); ].map((hex) => RGB.fromHex(hex).toLinear());
function decimate(input, palette, n) { function decimate(input, palette, n) {
// to brute-force, the possible palettes are: // to brute-force, the possible palettes are:
@ -535,7 +530,7 @@ function convert(source, sink) {
(line[x * 4 + 0] + line[x * 4 + 4]) / 2, (line[x * 4 + 0] + line[x * 4 + 4]) / 2,
(line[x * 4 + 1] + line[x * 4 + 5]) / 2, (line[x * 4 + 1] + line[x * 4 + 5]) / 2,
(line[x * 4 + 2] + line[x * 4 + 6]) / 2 (line[x * 4 + 2] + line[x * 4 + 6]) / 2
).sRGBToLinear(); ).toLinear();
if (nextError) { if (nextError) {
rgb.r += nextError.red[i]; rgb.r += nextError.red[i];
rgb.g += nextError.green[i]; rgb.g += nextError.green[i];
@ -549,7 +544,7 @@ function convert(source, sink) {
nextError = error; nextError = error;
for (let x = 0; x < width; x++) { for (let x = 0; x < width; x++) {
let rgb = palette[output[x >> 1]].linearTosRGB(); let rgb = palette[output[x >> 1]].fromLinear();
line[x * 4 + 0] = rgb.r; line[x * 4 + 0] = rgb.r;
line[x * 4 + 1] = rgb.g; line[x * 4 + 1] = rgb.g;
line[x * 4 + 2] = rgb.b; line[x * 4 + 2] = rgb.b;