Skip to content

Commit

Permalink
Try rebuilding the gamuts for Oklab.
Browse files Browse the repository at this point in the history
I'm not sure why before, only pure white had an issue...
  • Loading branch information
tommyettinger committed Sep 9, 2023
1 parent 619fd8e commit 11d2956
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void testPalette() {
if(!inGamut(L, A, B)){
System.out.printf("%s is having problems! It has L=%a,A=%a,B=%a\n", name, L, A, B);
hues.add(oklabHue(color));
analyzeFailure(L, A, B);
}
// if(inGamut(L, A, B)){
// System.out.printf("%s is doing fine. It has L=%f,A=%f,B=%f\n", name, L, A, B);
Expand All @@ -129,7 +130,7 @@ public void testPalette() {
}
}
public static double reverseLight(double L) {
return Math.pow(L, 2.0/3.0);
return Math.pow(L * (255.0/256.0), 2.0/3.0);
}

// public static double reverseLight(double L) {
Expand All @@ -154,9 +155,11 @@ public static double reverseLight(double L) {
public static boolean inGamut(double L, double A, double B)
{
//reverseLight() for double
L = reverseLight(L) * 0x0.ffp0;
// L = reverseLight(L);
L = reverseLight(L);
//forwardLight() for double
// L = (L - 1.00457) / (1.0 - L * 0.4285714) + 1.00457;

A -= 0x1.fdfdfep-2;
B -= 0x1.fdfdfep-2;

Expand All @@ -182,6 +185,39 @@ public static boolean inGamut(double L, double A, double B)
// return (b >= -0x1p-8 && b <= 0x101p-8);
}

public static boolean analyzeFailure(double L, double A, double B)
{
L = reverseLight(L);
A -= 0x1.fdfdfep-2;
B -= 0x1.fdfdfep-2;

double l = (L + +0.3963377774 * A + +0.2158037573 * B);
l *= l * l;
double m = (L + -0.1055613458 * A + -0.0638541728 * B);
m *= m * m;
double s = (L + -0.0894841775 * A + -1.2914855480 * B);
s *= s * s;

boolean valid = true;

final double r = +4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s;
if(r < 0.0 || r > 1.0) {
System.out.println("r out of range: " + r);
valid = false;
}
final double g = -1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s;
if(g < 0.0 || g > 1.0) {
System.out.println("g out of range: " + g);
valid = false;
}
final double b = -0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s;
if(b < 0.0 || b > 1.0) {
System.out.println("b out of range: " + b);
valid = false;
}
return valid;
}

@Test
public void testBlues() {
for (float f = 0.70934484f; f < 0.74934484f; f += 0.001f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void create() {
int idx = 0, largestDist = -1;
double minA = 1000.0, maxA = -1000.0, minB = 1000.0, maxB = -1000.0, maxDist = -1000.0, furthest = 300.0;
for (int light = 0; light < 256; light++) {
double L = light * 0x1p-8;
double L = light / 255.0;
PER_HUE:
for (int angle = 0; angle < 256; angle++) {
double theta = (angle) * 0x1p-7 * Math.PI;
Expand Down Expand Up @@ -103,6 +103,7 @@ public static void generateByteString(final byte[] data, String filename){
" static byte[] GAMUT_DATA;\n" +
" static {\n" +
" try {\n" +
" //noinspection StringBufferReplaceableByString\n" +
" GAMUT_DATA = new StringBuilder().append(\"");
for (int i = 0; i < data.length;) {
byte b = data[i++];
Expand Down Expand Up @@ -149,7 +150,7 @@ public static void generateByteString(final byte[] data, String filename){
System.out.println("Wrote code snippet to " + filename);
}
public static double reverseLight(double L) {
return Math.pow(L, 2.0/3.0);
return Math.pow(L * (255.0/256.0), 2.0/3.0);
}
// public static double reverseLight(double L) {
// L = Math.sqrt(L);
Expand Down Expand Up @@ -193,6 +194,7 @@ public static boolean inGamut(double L, double A, double B)
//reverseLight() for double
// L = (L - 0.993) / (1.0 + L * 0.75) + 0.993; // old
L = reverseLight(L);
// L = reverseLight(L * (255.0/256.0));

//unused:
//forwardLight() for double
Expand All @@ -212,12 +214,15 @@ public static boolean inGamut(double L, double A, double B)
// final double b = -0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s;
// return (b >= -0x1p-8 && b <= 0x101p-8);

final double r = +4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s;
if(r < 0.0 || r > 1.0) return false;
final double g = -1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s;
if(g < 0.0 || g > 1.0) return false;
final double b = -0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s;
return (b >= 0.0 && b <= 1.0);
double dr = Math.sqrt((+4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s)*255.999f);
final int r = (int)dr;
if(Double.isNaN(dr) || r < 0 || r > 255) return false;
double dg = Math.sqrt((-1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s)*255.999f);
final int g = (int)dg;
if(Double.isNaN(dg) || g < 0 || g > 255) return false;
double db = Math.sqrt((-0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s)*255.999f);
final int b = (int)db;
return (!Double.isNaN(db) && b >= 0 && b <= 255);


// return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255;
Expand Down

0 comments on commit 11d2956

Please sign in to comment.