Skip to content

Commit

Permalink
Implement and fixed color picker tool
Browse files Browse the repository at this point in the history
  • Loading branch information
RaydanOMGr committed Oct 26, 2024
1 parent e638e07 commit 6638abc
Showing 1 changed file with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.andreasmelone.glowingeyes.client.gui;

import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.logging.LogUtils;
Expand All @@ -17,8 +18,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.*;

import java.awt.*;
import java.util.HashMap;
Expand All @@ -43,6 +43,7 @@ public EyesEditorScreen() {
@Override
protected void init() {
super.init();
GL.createCapabilities();
this.guiLeft = (this.width - this.xSize) / 2;
this.guiTop = (this.height - this.ySize) / 2;

Expand Down Expand Up @@ -129,10 +130,10 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime)
if(pixels.containsKey(point)) {
Gui.fill(
poseStack,
headX + x * pixelSize + x * spaceBetweenPixels,
headY + y * pixelSize + y * spaceBetweenPixels,
headX + x * pixelSize + x * spaceBetweenPixels + pixelSize,
headY + y * pixelSize + y * spaceBetweenPixels + pixelSize,
headX + x * pixelSize + x * spaceBetweenPixels - 1,
headY + y * pixelSize + y * spaceBetweenPixels - 1,
headX + x * pixelSize + x * spaceBetweenPixels + pixelSize + 1,
headY + y * pixelSize + y * spaceBetweenPixels + pixelSize + 1,
pixels.get(point).getRGB()
);
}
Expand All @@ -151,6 +152,14 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime)
}
}

if(mode == Mode.PICKER && mouseX >= headX && mouseX <= endHeadX && mouseY >= headY && mouseY <= endHeadY) {
Color color = this.getPixelColor(mouseX, mouseY);

Gui.fill(poseStack, mouseX - 10, mouseY - 10 - 25, mouseX + 10, mouseY + 10 - 25, color.getRGB());
Gui.drawCenteredString(poseStack, minecraft.font, ColorUtil.intToHex(color.getRGB()),
mouseX, mouseY - 10, 0xFFFFFF);
}

super.render(poseStack, mouseX, mouseY, deltaTime);
}

Expand All @@ -176,23 +185,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
}

if (mode == Mode.PICKER && button == 0) {
GL.createCapabilities();
GL11.glReadBuffer(GL11.GL_FRONT);

float[] pixel = new float[4];

double scaleX = (double) Minecraft.getInstance().getWindow().getWidth() / (double) Minecraft.getInstance().getWindow().getGuiScaledWidth();
double scaleY = (double) Minecraft.getInstance().getWindow().getHeight() / (double) Minecraft.getInstance().getWindow().getGuiScaledHeight();

GL11.glReadPixels(
(int)(mouseX * scaleX), (int) (mouseY * scaleY),
1, 1,
GL11.GL_RGBA, GL11.GL_FLOAT,
pixel
);

Color color = new Color(pixel[0], pixel[1], pixel[2], pixel[3]);
System.out.println("Color: " + ColorUtil.intToHex(color.getRGB()));
Color color = this.getPixelColor(mouseX, mouseY);
ColorPickerScreen.setSelectedColor(color);

modeButtons.get(Mode.BRUSH).onPress();
Expand Down Expand Up @@ -231,6 +224,32 @@ public void openAsParent() {
Minecraft.getInstance().setScreen(this);
}

private Color getPixelColor(double x, double y) {
Window window = minecraft.getWindow();
if (x < 0 || x > window.getWidth()) {
throw new IllegalArgumentException("x must be within the screen width: 0 to " + window.getWidth() + ". Provided: " + x);
}
if (y < 0 || y > window.getHeight()) {
throw new IllegalArgumentException("y must be within the screen height: 0 to " + window.getHeight() + ". Provided: " + y);
}

float[] pixel = new float[4];

// Divides the actual width/height by the scaled width/height to find out by what factor it was scaled
double scaleX = (double) window.getWidth() / window.getGuiScaledWidth();
double scaleY = (double) window.getHeight() / window.getGuiScaledHeight();
// Calculates the actual position of the pixel
int pixelX = (int) (x * scaleX);
int pixelY = (int) ((window.getGuiScaledHeight() - y) * scaleY); // The y value needs
// to be inverted relative to the height
// since minecraft's 0-point is top-left
// while gl's 0-point is bottom-left
GL11.glReadPixels(pixelX, pixelY, 1, 1, GL11.GL_RGBA, GL11.GL_FLOAT, pixel);

return new Color(pixel[0], pixel[1], pixel[2], pixel[3]);
}


private void calculateHeadSize(int headSize, int pixelSize, int spaceBetweenPixels) {
int head = headSize * pixelSize + (headSize - 1) * spaceBetweenPixels;
headX = this.guiLeft + (this.xSize - head) / 2;
Expand Down

0 comments on commit 6638abc

Please sign in to comment.