Created
December 4, 2023 20:25
-
-
Save jandk/e357070085f604fbc8d33d52e0575f9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.twofold.test; | |
import java.util.function.*; | |
public class Main { | |
public static void main(String[] args) { | |
int diff61 = testFullRange((a, b) -> compare6(a, b, 1)); | |
int diff63 = testFullRange((a, b) -> compare6(a, b, 3)); | |
int diff41 = testFullRange((a, b) -> compare4(a, b, 1)); | |
int diff42 = testFullRange((a, b) -> compare4(a, b, 2)); | |
System.out.println("6:1: " + diff61); | |
System.out.println("6:3: " + diff63); | |
System.out.println("4:1: " + diff41); | |
System.out.println("4:2: " + diff42); | |
} | |
private static int testFullRange(BiPredicate<Integer, Integer> compare) { | |
int differenceCount = 0; | |
for (int a = 0; a < 256; a++) { | |
for (int b = 0; b < 256; b++) { | |
differenceCount += compare.test(a, b) ? 1 : 0; | |
} | |
} | |
return differenceCount; | |
} | |
private static boolean compare6(int a, int b, int offset) { | |
float fa = fromUNorm8(a); | |
float fb = fromUNorm8(b); | |
int agt2 = (6 * a + 1 * b + offset) / 7; | |
int agt3 = (5 * a + 2 * b + offset) / 7; | |
int agt4 = (4 * a + 3 * b + offset) / 7; | |
int agt5 = (3 * a + 4 * b + offset) / 7; | |
int agt6 = (2 * a + 5 * b + offset) / 7; | |
int agt7 = (1 * a + 6 * b + offset) / 7; | |
int egt2 = toUNorm8(lerp(fa, fb, 1f / 7f)); | |
int egt3 = toUNorm8(lerp(fa, fb, 2f / 7f)); | |
int egt4 = toUNorm8(lerp(fa, fb, 3f / 7f)); | |
int egt5 = toUNorm8(lerp(fa, fb, 4f / 7f)); | |
int egt6 = toUNorm8(lerp(fa, fb, 5f / 7f)); | |
int egt7 = toUNorm8(lerp(fa, fb, 6f / 7f)); | |
boolean difference = false; | |
difference |= agt2 != egt2; | |
difference |= agt3 != egt3; | |
difference |= agt4 != egt4; | |
difference |= agt5 != egt5; | |
difference |= agt6 != egt6; | |
difference |= agt7 != egt7; | |
return difference; | |
} | |
private static boolean compare4(int a, int b, int offset) { | |
float fa = fromUNorm8(a); | |
float fb = fromUNorm8(b); | |
int agt2 = (4 * a + 1 * b + offset) / 5; | |
int agt3 = (3 * a + 2 * b + offset) / 5; | |
int agt4 = (2 * a + 3 * b + offset) / 5; | |
int agt5 = (1 * a + 4 * b + offset) / 5; | |
int egt2 = toUNorm8(lerp(fa, fb, 1f / 5f)); | |
int egt3 = toUNorm8(lerp(fa, fb, 2f / 5f)); | |
int egt4 = toUNorm8(lerp(fa, fb, 3f / 5f)); | |
int egt5 = toUNorm8(lerp(fa, fb, 4f / 5f)); | |
boolean difference = false; | |
difference |= agt2 != egt2; | |
difference |= agt3 != egt3; | |
difference |= agt4 != egt4; | |
difference |= agt5 != egt5; | |
return difference; | |
} | |
private static float clamp(float value, float min, float max) { | |
return Math.min(max, Math.max(value, min)); | |
} | |
private static float lerp(float a, float b, float t) { | |
return a + (b - a) * t; | |
} | |
private static float fromUNorm8(int value) { | |
return value / 255f; | |
} | |
private static int toUNorm8(float value) { | |
return (int) (clamp(value, 0f, 1f) * 255f + .5f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment