Skip to content

Commit 8ac87a9

Browse files
committed
Add ImColor helper. Increase flexibility of the color API
In most of the places it's now possible to provide a simple int value, instead of float numbers.
1 parent 71e7265 commit 8ac87a9

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package imgui;
2+
3+
/**
4+
* Helper class to get ABGR packed color used by Dear ImGui.
5+
*/
6+
public final class ImColor {
7+
private ImColor() {
8+
}
9+
10+
public static int get32(final int r, final int g, final int b, final int a) {
11+
return a << 24 | b << 16 | g << 8 | r;
12+
}
13+
14+
public static int get32(final int r, final int g, final int b) {
15+
return get32(r, g, b, 255);
16+
}
17+
18+
public static int get32(final float r, final float g, final float b, final float a) {
19+
return get32((int) (r * 255), (int) (g * 255), (int) (b * 255), (int) (a * 255));
20+
}
21+
22+
public static int get32(final float r, final float g, final float b) {
23+
return get32(r, g, b, 1f);
24+
}
25+
26+
/**
27+
* @param hex e.g. "#FFFFFF"
28+
*/
29+
public static int get32RGBHex(final String hex) {
30+
return get32(
31+
Integer.parseInt(hex.substring(1, 3), 16),
32+
Integer.parseInt(hex.substring(3, 5), 16),
33+
Integer.parseInt(hex.substring(5, 7), 16)
34+
);
35+
}
36+
37+
/**
38+
* @param hex e.g. "#FFFFFFFF"
39+
*/
40+
public static int get32RGBAHex(final String hex) {
41+
return get32(
42+
Integer.parseInt(hex.substring(1, 3), 16),
43+
Integer.parseInt(hex.substring(3, 5), 16),
44+
Integer.parseInt(hex.substring(5, 7), 16),
45+
Integer.parseInt(hex.substring(7, 9), 16)
46+
);
47+
}
48+
}

imgui-binding/src/main/java/imgui/ImGui.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,16 @@ public static void pushFont(ImFont font) {
877877
ImGui::PopFont();
878878
*/
879879

880-
public static native void pushStyleColor(int imGuiCol, int col); /*
881-
ImGui::PushStyleColor(imGuiCol, col);
880+
public static native void pushStyleColor(int imGuiCol, float r, float g, float b, float a); /*
881+
ImGui::PushStyleColor(imGuiCol, (ImU32)ImColor(r, g, b, a));
882882
*/
883883

884-
public static native void pushStyleColor(int imGuiCol, float r, float g, float b, float a); /*
885-
ImGui::PushStyleColor(imGuiCol, ImVec4(r, g, b, a));
884+
public static native void pushStyleColor(int imGuiCol, int r, int g, int b, int a); /*
885+
ImGui::PushStyleColor(imGuiCol, (ImU32)ImColor(r, g, b, a));
886+
*/
887+
888+
public static native void pushStyleColor(int imGuiCol, int col); /*
889+
ImGui::PushStyleColor(imGuiCol, col);
886890
*/
887891

888892
public static native void popStyleColor(); /*
@@ -1325,7 +1329,21 @@ public static ImFont getFont() {
13251329
* Shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor();
13261330
*/
13271331
public static native void textColored(float r, float g, float b, float a, String text); /*
1328-
ImGui::TextColored(ImVec4(r, g, b, a), text, NULL);
1332+
ImGui::TextColored(ImColor(r, g, b, a), text, NULL);
1333+
*/
1334+
1335+
/**
1336+
* Shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor();
1337+
*/
1338+
public static native void textColored(int r, int g, int b, int a, String text); /*
1339+
ImGui::TextColored(ImColor(r, g, b, a), text, NULL);
1340+
*/
1341+
1342+
/**
1343+
* Shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor();
1344+
*/
1345+
public static native void textColored(int col, String text); /*
1346+
ImGui::TextColored(ImColor(col), text, NULL);
13291347
*/
13301348

13311349
/**

imgui-binding/src/main/java/imgui/ImGuiStyle.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,15 @@ public void destroy() {
602602
*/
603603

604604
public native void setColor(int imGuiCol, float r, float g, float b, float a); /*
605-
IMGUI_STYLE->Colors[imGuiCol] = ImVec4(r, g, b, a);
605+
IMGUI_STYLE->Colors[imGuiCol] = ImColor(r, g, b, a);
606+
*/
607+
608+
public native void setColor(int imGuiCol, int r, int g, int b, int a); /*
609+
IMGUI_STYLE->Colors[imGuiCol] = ImColor(r, g, b, a);
610+
*/
611+
612+
public native void setColor(int imGuiCol, int col); /*
613+
IMGUI_STYLE->Colors[imGuiCol] = ImColor(col);
606614
*/
607615

608616
public native void scaleAllSizes(float scaleFactor); /*

imgui-lwjgl3/src/test/java/ExampleUi.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import imgui.ImBool;
2+
import imgui.ImColor;
23
import imgui.ImGui;
34
import imgui.ImString;
45
import imgui.ImVec2;
@@ -18,6 +19,10 @@
1819
final class ExampleUi {
1920
private static final String IMGUI_DEMO_LINK = "https://raw.githubusercontent.com/ocornut/imgui/v1.76/imgui_demo.cpp";
2021

22+
private static final int DODGERBLUE_COLOR = ImColor.get32RGBHex("#1E90FF");
23+
private static final int CORAL_COLOR = ImColor.get32RGBHex("#FF7F50");
24+
private static final int LIMEGREEN_COLOR = ImColor.get32RGBHex("#32CD32");
25+
2126
// Test data for payload
2227
private final byte[] testPayload = "Test Payload".getBytes();
2328
private String dropTargetText = "Drop Here";
@@ -86,19 +91,19 @@ void render() {
8691
ImGui.inputText("Resizable input", resizableStr, ImGuiInputTextFlags.CallbackResize);
8792
ImGui.text("text len:");
8893
ImGui.sameLine();
89-
ImGui.textColored(.12f, .6f, 1, 1, Integer.toString(resizableStr.getLength()));
94+
ImGui.textColored(DODGERBLUE_COLOR, Integer.toString(resizableStr.getLength()));
9095
ImGui.sameLine();
9196
ImGui.text("| buffer size:");
9297
ImGui.sameLine();
93-
ImGui.textColored(1, .6f, 0, 1, Integer.toString(resizableStr.getBufferSize()));
98+
ImGui.textColored(CORAL_COLOR, Integer.toString(resizableStr.getBufferSize()));
9499

95100
ImGui.separator();
96101
ImGui.newLine();
97102

98103
// Link to the original demo file
99104
ImGui.text("Consider to look the original ImGui demo: ");
100105
ImGui.setNextItemWidth(500);
101-
ImGui.textColored(0, .8f, 0, 1, IMGUI_DEMO_LINK);
106+
ImGui.textColored(LIMEGREEN_COLOR, IMGUI_DEMO_LINK);
102107
ImGui.sameLine();
103108
if (ImGui.button("Copy")) {
104109
ImGui.setClipboardText(IMGUI_DEMO_LINK);

0 commit comments

Comments
 (0)