From 47efa43de21ca7d624206cadc5e6b41e5a1ab12a Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Tue, 17 Nov 2020 01:29:47 +0100 Subject: [PATCH] Color4f.toColor --- .../java/org/jetbrains/skija/Color4f.java | 15 ++++++++--- .../java/org/jetbrains/skija/ColorTest.java | 27 +++++++++++++++++++ .../java/org/jetbrains/skija/TestSuite.java | 1 + 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 shared/src/test/java/org/jetbrains/skija/ColorTest.java diff --git a/shared/src/main/java/org/jetbrains/skija/Color4f.java b/shared/src/main/java/org/jetbrains/skija/Color4f.java index 77013a20..1601c4a0 100644 --- a/shared/src/main/java/org/jetbrains/skija/Color4f.java +++ b/shared/src/main/java/org/jetbrains/skija/Color4f.java @@ -20,10 +20,17 @@ public Color4f(float[] rgba) { } public Color4f(int c) { - this((c >> 16 & 0xFF) / 256f, - (c >> 8 & 0xFF) / 256f, - (c & 0xFF) / 256f, - (c >> 24 & 0xFF) / 256f); + this((c >> 16 & 0xFF) / 255f, + (c >> 8 & 0xFF) / 255f, + (c & 0xFF) / 255f, + (c >> 24 & 0xFF) / 255f); + } + + public int toColor() { + return (((int) (_a * 255f)) << 24) + | (((int) (_r * 255f)) << 16) + | (((int) (_g * 255f)) << 8) + | ((int) (_b * 255f)); } public float[] flatten() { diff --git a/shared/src/test/java/org/jetbrains/skija/ColorTest.java b/shared/src/test/java/org/jetbrains/skija/ColorTest.java new file mode 100644 index 00000000..92fa73f3 --- /dev/null +++ b/shared/src/test/java/org/jetbrains/skija/ColorTest.java @@ -0,0 +1,27 @@ +package org.jetbrains.skija; + +import java.util.*; +import org.jetbrains.skija.test.*; +import static org.jetbrains.skija.test.TestRunner.*; + +public class ColorTest implements Executable { + @Override + public void execute() throws Exception { + Map cases = new HashMap<>(); + cases.put(0x00000000, new Color4f(0, 0, 0, 0)); + cases.put(0xFF000000, new Color4f(0, 0, 0, 1)); + cases.put(0x00FF0000, new Color4f(1, 0, 0, 0)); + cases.put(0x0000FF00, new Color4f(0, 1, 0, 0)); + cases.put(0x000000FF, new Color4f(0, 0, 1, 0)); + cases.put(0x80808080, new Color4f(128/255f, 128/255f, 128/255f, 128/255f)); + + for (var entry: cases.entrySet()) { + int color = entry.getKey(); + Color4f color4f = entry.getValue(); + pushStack(Integer.toString(color, 16) + " <-> " + color4f); + assertEquals(color, color4f.toColor()); + assertEquals(new Color4f(color), color4f); + popStack(); + } + } +} \ No newline at end of file diff --git a/shared/src/test/java/org/jetbrains/skija/TestSuite.java b/shared/src/test/java/org/jetbrains/skija/TestSuite.java index 8181e786..fd559699 100644 --- a/shared/src/test/java/org/jetbrains/skija/TestSuite.java +++ b/shared/src/test/java/org/jetbrains/skija/TestSuite.java @@ -7,6 +7,7 @@ public class TestSuite { public static void main(String[] args) { TestRunner.startTesting(); TestRunner.testClass(BitmapTest.class); + TestRunner.testClass(ColorTest.class); TestRunner.testClass(DataTest.class); TestRunner.testClass(FontMgrTest.class); TestRunner.testClass(ImageTest.class);