Skip to content

Commit 001b4ad

Browse files
committed
feat: add transparent colors
1 parent 840986f commit 001b4ad

File tree

8 files changed

+68
-35
lines changed

8 files changed

+68
-35
lines changed

examples/screen/src/main/kotlin/dev/vexide/hydrozoa/examples/screen/Main.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package dev.vexide.hydrozoa.examples.screen
22

33
import dev.vexide.hydrozoa.Peripherals
44
import dev.vexide.hydrozoa.Platform
5+
import dev.vexide.hydrozoa.display.Color
56
import dev.vexide.hydrozoa.display.Rect
6-
import dev.vexide.hydrozoa.display.Rgb
77
import dev.vexide.hydrozoa.display.Text
88

99
fun main() {
1010
val peripherals = Peripherals.take().orElseThrow()
1111
val display = peripherals.takeDisplay()
1212

1313
val rect = Rect(20, 20, 100, 100)
14-
display.fill(rect, Rgb.fromInteger(0xFFFFFF))
15-
display.stroke(rect, Rgb.fromInteger(0xFF00FF))
14+
display.fill(rect, Color.WHITE)
15+
display.stroke(rect, Color.FUCHSIA)
1616

1717
val text = Text("Nice to see you!", 80, 80)
18-
display.write(text, Rgb.fromInteger(0x00FFFF), Rgb.fromInteger(0x000000))
18+
display.write(text, Color.OLIVE, Color.TRANSPARENT)
1919

2020
while (true) {
2121
Platform.yield()

src/main/java/dev/vexide/hydrozoa/display/Circle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public record Circle(int x, int y, int radius) implements Shape {
1111
}
1212

1313
@Override
14-
public void draw(@NotNull Display screen, @NotNull Rgb color, boolean fill) {
15-
VexSdk.Display.vexDisplayForegroundColor(color.toInteger());
14+
public void draw(@NotNull Display screen, @NotNull Color color, boolean fill) {
15+
VexSdk.Display.vexDisplayForegroundColor(color.raw());
1616
if (fill) {
1717
VexSdk.Display.vexDisplayCircleFill(x, y + Display.HEADER_HEIGHT, radius);
1818
} else {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.vexide.hydrozoa.display;
2+
3+
import org.jetbrains.annotations.Contract;
4+
5+
public record Color(int raw) {
6+
public static final Color TRANSPARENT = new Color(0, true);
7+
public static final Color BLACK = new Color(0x000000);
8+
public static final Color SILVER = new Color(0xC0C0C0);
9+
public static final Color GRAY = new Color(0x808080);
10+
public static final Color WHITE = new Color(0xFFFFFF);
11+
public static final Color MAROON = new Color(0x800000);
12+
public static final Color RED = new Color(0xFF0000);
13+
public static final Color PURPLE = new Color(0x800080);
14+
public static final Color FUCHSIA = new Color(0xFF00FF);
15+
public static final Color GREEN = new Color(0x008000);
16+
public static final Color LIME = new Color(0x00FF00);
17+
public static final Color OLIVE = new Color(0x808000);
18+
public static final Color YELLOW = new Color(0xFFFF00);
19+
public static final Color NAVY = new Color(0x000080);
20+
public static final Color BLUE = new Color(0x0000FF);
21+
public static final Color TEAL = new Color(0x008080);
22+
public static final Color AQUA = new Color(0x00FFFF);
23+
public Color(int red, int green, int blue) {
24+
this(red, green, blue, false);
25+
}
26+
public Color(int red, int green, int blue, boolean transparent) {
27+
this((red << 16) | (green << 8) | blue, transparent);
28+
}
29+
public Color(int raw, boolean transparent) {
30+
this(raw | (transparent ? 0x1000000 : 0));
31+
}
32+
33+
@Contract(pure = true)
34+
public boolean transparent() {
35+
return (raw & 0x1000000) != 0;
36+
}
37+
38+
@Contract(pure = true)
39+
public int red() {
40+
return (raw >> 16) & 0xFF;
41+
}
42+
43+
@Contract(pure = true)
44+
public int green() {
45+
return (raw >> 8) & 0xFF;
46+
}
47+
48+
@Contract(pure = true)
49+
public int blue() {
50+
return raw & 0xFF;
51+
}
52+
}

src/main/java/dev/vexide/hydrozoa/display/Display.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public void render() {
3030
}
3131
}
3232

33-
public void draw(@NotNull Shape shape, @NotNull Rgb color, boolean fill) {
33+
public void draw(@NotNull Shape shape, @NotNull Color color, boolean fill) {
3434
shape.draw(this, color, fill);
3535
}
3636

37-
public void fill(@NotNull Shape shape, @NotNull Rgb color) {
37+
public void fill(@NotNull Shape shape, @NotNull Color color) {
3838
shape.draw(this, color, true);
3939
}
4040

41-
public void stroke(@NotNull Shape shape, @NotNull Rgb color) {
41+
public void stroke(@NotNull Shape shape, @NotNull Color color) {
4242
shape.draw(this, color, false);
4343
}
4444

45-
public void write(@NotNull Text text, @NotNull Rgb fgColor, @NotNull Rgb bgColor) {
45+
public void write(@NotNull Text text, @NotNull Color fgColor, @NotNull Color bgColor) {
4646
text.draw(this, fgColor, bgColor);
4747
}
4848

src/main/java/dev/vexide/hydrozoa/display/Rect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public record Rect(int x1, int y1, int x2, int y2) implements Shape {
1616
}
1717

1818
@Override
19-
public void draw(@NotNull Display screen, @NotNull Rgb color, boolean fill) {
20-
VexSdk.Display.vexDisplayForegroundColor(color.toInteger());
19+
public void draw(@NotNull Display screen, @NotNull Color color, boolean fill) {
20+
VexSdk.Display.vexDisplayForegroundColor(color.raw());
2121
if (fill) {
2222
VexSdk.Display.vexDisplayRectFill(x1, y1 + Display.HEADER_HEIGHT, x2, y2 + Display.HEADER_HEIGHT);
2323
} else {

src/main/java/dev/vexide/hydrozoa/display/Rgb.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/dev/vexide/hydrozoa/display/Shape.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import org.jetbrains.annotations.NotNull;
44

55
public interface Shape {
6-
void draw(@NotNull Display screen, @NotNull Rgb color, boolean fill);
6+
void draw(@NotNull Display screen, @NotNull Color color, boolean fill);
77
}

src/main/java/dev/vexide/hydrozoa/display/Text.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public int width() {
4949
return VexSdk.Display.vexDisplayStringWidthGet(text);
5050
}
5151

52-
public void draw(@NotNull Display display, @NotNull Rgb fgColor, @NotNull Rgb bgColor) {
53-
VexSdk.Display.vexDisplayForegroundColor(fgColor.toInteger());
54-
VexSdk.Display.vexDisplayBackgroundColor(bgColor.toInteger());
52+
public void draw(@NotNull Display display, @NotNull Color fgColor, @NotNull Color bgColor) {
53+
VexSdk.Display.vexDisplayForegroundColor(fgColor.raw());
54+
VexSdk.Display.vexDisplayBackgroundColor(bgColor.raw());
5555

5656
var x = switch (hAlign) {
5757
case Left -> this.x;

0 commit comments

Comments
 (0)