Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Last active March 31, 2024 16:15
Show Gist options
  • Save Densamisten/77863e20ff49c544eac38ac31206104f to your computer and use it in GitHub Desktop.
Save Densamisten/77863e20ff49c544eac38ac31206104f to your computer and use it in GitHub Desktop.
Gets color of a block in Fabric 1.20.[1-2-3-4] and prints its RGB, HSL, CMYK, HEX and decimal value.
package io.github.densamisten.color_picker.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.BlockState;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.glfw.GLFW;
public class ColorPickerClient implements ClientModInitializer {
private boolean useBlockCallbackEnabled = true;
private static final KeyBinding toggleUseBlockCallback = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.colorpicker.toggle",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN, // Change this to the desired key
"category.colorpicker"
));
@Override
public void onInitializeClient() {
// Register UseBlockCallback
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (hitResult.getType() == HitResult.Type.BLOCK) {
if (toggleUseBlockCallback.wasPressed()) {
useBlockCallbackEnabled = !useBlockCallbackEnabled;
if (useBlockCallbackEnabled) {
player.sendMessage(Text.of("UseBlockCallback enabled."), false);
} else {
player.sendMessage(Text.of("UseBlockCallback disabled."), false);
}
}
if (useBlockCallbackEnabled) {
// Check if the player is looking at a block
BlockPos blockPos = hitResult.getBlockPos();
BlockState blockState = world.getBlockState(blockPos);
// Get the color of the block
int blockColor = blockState.getBlock().getDefaultMapColor().color;
// Convert color to RGB, HSL, and CMYK
int red = (blockColor >> 16) & 0xFF;
int green = (blockColor >> 8) & 0xFF;
int blue = blockColor & 0xFF;
// RGB to HEX conversion
String hex = String.format("#%02X%02X%02X", red, green, blue);
// RGB to HSL conversion
double[] hsl = rgbToHsl(red, green, blue);
double hue = hsl[0];
double saturation = hsl[1];
double lightness = hsl[2];
// RGB to CMYK conversion
double[] cmyk = rgbToCmyk(red, green, blue);
double cyan = cmyk[0];
double magenta = cmyk[1];
double yellow = cmyk[2];
double black = cmyk[3];
// Print information about the block color to the console
String concatenatedString = red + ", " + green + ", " + blue;
Text rgbInfo = Text.literal("RGB: " + red + "," + green + "," + blue).styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, concatenatedString)));
Text hexInfo = Text.literal("HEX: " + hex).styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, hex)));
Text hslInfo = Text.literal("HSL: Hue: " + hue + ", Saturation: " + saturation + ", Lightness: " + lightness).styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "Hue: " + hue + ", Saturation: " + saturation + ", Lightness: " + lightness)));
String concatenatedCmykString = cyan + ", " + magenta + ", " + yellow + ", " + black;
Text cmykInfo = Text.literal("CMYK: Cyan: " + cyan + ", Magenta: " + magenta + ", Yellow: " + yellow + ", Black: " + black).styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, concatenatedCmykString)));
Text decimalInfo = Text.literal("DECIMAL: " + blockColor).styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, String.valueOf(blockColor))));
player.sendMessage(rgbInfo);
player.sendMessage(hexInfo);
player.sendMessage(hslInfo);
player.sendMessage(cmykInfo);
player.sendMessage(decimalInfo);
return ActionResult.SUCCESS; // Consume the interaction if UseBlockCallback is enabled
}
}
return ActionResult.PASS; // Allow other mods to handle the interaction
});
}
// Helper method to convert RGB to HSL
private static double[] rgbToHsl(int r, int g, int b) {
double hue, saturation, lightness;
double red = r / 255.0;
double green = g / 255.0;
double blue = b / 255.0;
double max = Math.max(red, Math.max(green, blue));
double min = Math.min(red, Math.min(green, blue));
// Calculating the hue
if (max == min) {
hue = 0; // Undefined hue
} else if (max == red) {
hue = ((green - blue) / (max - min)) % 6;
} else if (max == green) {
hue = (blue - red) / (max - min) + 2;
} else {
hue = (red - green) / (max - min) + 4;
}
hue *= 60;
// Adjusting the negative hue
if (hue < 0) {
hue += 360;
}
// Calculating the lightness
lightness = (max + min) / 2;
// Calculating the saturation
if (max == min) {
saturation = 0;
} else if (lightness <= 0.5) {
saturation = (max - min) / (max + min);
} else {
saturation = (max - min) / (2 - (max + min));
}
return new double[]{hue, saturation, lightness};
}
// Helper method to convert RGB to CMYK
private static double[] rgbToCmyk(int r, int g, int b) {
double cyan, magenta, yellow, black;
double red = r / 255.0;
double green = g / 255.0;
double blue = b / 255.0;
black = 1 - Math.max(red, Math.max(green, blue));
if (black == 1) {
cyan = 0;
magenta = 0;
yellow = 0;
} else {
cyan = (1 - red - black) / (1 - black);
magenta = (1 - green - black) / (1 - black);
yellow = (1 - blue - black) / (1 - black);
}
return new double[]{cyan, magenta, yellow, black};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment