Skip to content

Commit

Permalink
Color4f.toColor
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Nov 17, 2020
1 parent b05e8f8 commit 47efa43
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
15 changes: 11 additions & 4 deletions shared/src/main/java/org/jetbrains/skija/Color4f.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
27 changes: 27 additions & 0 deletions shared/src/test/java/org/jetbrains/skija/ColorTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Color4f> 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();
}
}
}
1 change: 1 addition & 0 deletions shared/src/test/java/org/jetbrains/skija/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 47efa43

Please sign in to comment.