Skip to content

Commit

Permalink
Fixed colour bug and improved colour picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon3055 committed Apr 21, 2024
1 parent fe58ede commit 3a47b58
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/main/java/codechicken/lib/colour/Colour.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,19 @@ public Colour aF(int a) {
}

public float rF() {
return r / 255F;
return r() / 255F;
}

public float gF() {
return g / 255F;
return g() / 255F;
}

public float bF() {
return b / 255F;
return b() / 255F;
}

public float aF() {
return a / 255F;
return a() / 255F;
}

public int r() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@
public class GuiColourPicker extends GuiManipulable {

private ColourState colourState = ColourState.create();
private final Colour initialColour;
private boolean blockOutsideClicks = false;
private boolean closeOnOutsideClick = false;
private GuiButton okButton;
private GuiButton cancelButton;

public GuiColourPicker(@NotNull GuiParent<?> parent) {
public GuiColourPicker(@NotNull GuiParent<?> parent, Colour initialColour) {
super(parent);
this.initialColour = initialColour;
}

public static GuiColourPicker create(GuiParent<?> guiParent, ColourState colourState) {
return create(guiParent, colourState, true);
}

public static GuiColourPicker create(GuiParent<?> guiParent, ColourState colourState, boolean hasAlpha) {
Colour initialColour = colourState.getColour();
GuiColourPicker picker = new GuiColourPicker(guiParent.getModularGui().getRoot());
GuiColourPicker picker = new GuiColourPicker(guiParent.getModularGui().getRoot(), colourState.getColour());
picker.setOpaque(true);
picker.setColourState(colourState);
Constraints.size(picker, 80, hasAlpha ? 80 : 68);
Constraints.size(picker, 80, hasAlpha ? 80 : 70);

GuiRectangle background = GuiRectangle.toolTipBackground(picker.getContentElement());
Constraints.bind(background, picker.getContentElement());
Expand Down Expand Up @@ -77,10 +80,7 @@ public static GuiColourPicker create(GuiParent<?> guiParent, ColourState colourS

picker.cancelButton = GuiButton.flatColourButton(background, () -> Component.translatable("gui.cancel"), e -> 0xFF000000, e -> e ? 0xFF777777 : 0xFF555555)
.setOpaque(true)
.onPress(() -> {
colourState.set(initialColour);
picker.getParent().removeChild(picker);
})
.onPress(picker::cancel)
.constrain(HEIGHT, literal(10))
.constrain(TOP, relative(preview.get(BOTTOM), 2))
.constrain(LEFT, midPoint(background.get(LEFT), background.get(RIGHT), -4))
Expand Down Expand Up @@ -121,6 +121,35 @@ public static SliderBG makeSlider(GuiElement<?> background, int colour, SliderSt
return slideBG;
}

public GuiColourPicker setBlockOutsideClicks(boolean blockOutsideClicks) {
this.blockOutsideClicks = blockOutsideClicks;
return this;
}

public GuiColourPicker setCancelOnOutsideClick(boolean closeOnOutsideClick) {
this.closeOnOutsideClick = closeOnOutsideClick;
return this;
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button, boolean consumed) {
if (closeOnOutsideClick && !getContentElement().getRectangle().contains(mouseX, mouseY)) {
cancel();
}
return super.mouseClicked(mouseX, mouseY, button, consumed) || blockOutsideClicks;
}

public void cancel() {
if (cancelButton != null && cancelButton.isEnabled()){
colourState.set(initialColour);
}
close();
}

public void close() {
getParent().removeChild(this);
}

private static boolean validHex(String value) {
try {
Integer.parseUnsignedInt(value, 16);
Expand Down

0 comments on commit 3a47b58

Please sign in to comment.