From fe6314e2a0852959172a0d1ae7f1ccb79c4f16a5 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Fri, 24 Mar 2023 00:28:07 -0700 Subject: [PATCH] just use srgb for consistency with the pngs --- dither-image.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/dither-image.js b/dither-image.js index ee313d0..2c9e252 100644 --- a/dither-image.js +++ b/dither-image.js @@ -13,6 +13,7 @@ function zeroes(n) { return arr; } +/* function toLinear(val) { // use a 2.4 gamma approximation // this is BT.1886 compatible @@ -27,6 +28,18 @@ function fromLinear(val) { unit **= (1 / 2.4); return unit * 255; } +*/ + +function fromSRGB(val) { + val /= 255; + if (val <= 0.04045) { + val /= 12.92; + } else { + val = ((val + 0.055) / 1.055) ** 2.4; + } + val *= 255; + return val; +} function toSRGB(val) { val /= 255; @@ -65,6 +78,7 @@ class RGB { ); } + /* toLinear() { return this.map(toLinear); } @@ -72,6 +86,11 @@ class RGB { fromLinear() { return this.map(fromLinear); } + */ + + fromSRGB() { + return this.map(fromSRGB); + } toSRGB() { return this.map(toSRGB); @@ -410,7 +429,7 @@ let atariRGB = [ 0xf6e46f, 0xfffa84, 0xffff99, -].map((hex) => RGB.fromHex(hex).toLinear()); +].map((hex) => RGB.fromHex(hex).fromSRGB()); //].map((hex) => RGB.fromHex(hex)); /** @@ -667,7 +686,7 @@ function imageToLinearRGB(rgba) { rgba[i + 0], rgba[i + 1], rgba[i + 2] - ).toLinear()); + ).fromSRGB()); } return input; }