From e48cd4479f832cdd341ab0a289d1b4a88ab21a3c Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Wed, 1 Jun 2022 17:37:49 +0100 Subject: OC-53 should be done but had to push + color utils changed again --- .../cc/polyfrost/oneconfig/utils/ColorUtils.java | 255 --------------------- .../oneconfig/utils/color/ColorPalette.java | 140 +++++++++++ .../oneconfig/utils/color/ColorUtils.java | 183 +++++++++++++++ 3 files changed, 323 insertions(+), 255 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java (limited to 'src/main/java/cc/polyfrost/oneconfig/utils') diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java deleted file mode 100644 index 127b200..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java +++ /dev/null @@ -1,255 +0,0 @@ -package cc.polyfrost.oneconfig.utils; - -import cc.polyfrost.oneconfig.config.OneConfigConfig; - -import java.awt.*; - -/** - * A class to help with color manipulation. - */ -public final class ColorUtils { - /** - * Always returns transparent. - */ - public static final int TRANSPARENT = -10; - /** - *

Primary Color Scheme

Normal: Primary 600,
Hover: Primary 700,
Clicked: Primary 700 (80%) - */ - public static final int PRIMARY = 1; - /** - *

Secondary Color Scheme

Normal: Gray 500,
Hover: Gray 400,
Clicked: Gray 400 (80%) - */ - public static final int SECONDARY = 2; - /** - *

Secondary (Transparent) Color Scheme

Normal: Transparent,
Hover: Gray rgba(229, 229, 229, 77),
Clicked: Gray rgba(229, 229, 229, 51) - */ - public static final int SECONDARY_TRANSPARENT = 0; - /** - *

Tertiary Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=White 100%),
Clicked: Transparent (Text=White 80%) - *

NOTICE this returns the text colors as it is always transparent.

- */ - public static final int TERTIARY = 3; - /** - *

Primary Destructive Color Scheme

Normal: Error 700,
Hover: Error 600,
Clicked: Error 600 (80%) - */ - public static final int PRIMARY_DESTRUCTIVE = -1; - /** - *

Secondary Destructive Color Scheme

Normal: Gray 500,
Hover: Error 800,
Clicked: Error 800 (80%) - */ - public static final int SECONDARY_DESTRUCTIVE = -2; - /** - *

Tertiary Destructive Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=Error 300),
Clicked: Transparent (Text=Error 300 80%) - *

NOTICE this returns the text colors as it is always transparent.

- */ - public static final int TERTIARY_DESTRUCTIVE = -3; - - - public static int getColor(int currentColor, int colorPalette, boolean hover, boolean click) { - float[] color = splitColor(currentColor); - if (colorPalette == TRANSPARENT) { - return OneConfigConfig.TRANSPARENT; - } - if (click) { - switch (colorPalette) { - case PRIMARY_DESTRUCTIVE: - return OneConfigConfig.ERROR_600_80; - case SECONDARY_DESTRUCTIVE: - return OneConfigConfig.ERROR_800_80; - case TERTIARY_DESTRUCTIVE: - return OneConfigConfig.ERROR_300_80; - case TERTIARY: - return OneConfigConfig.WHITE_80; - default: - case SECONDARY: - return OneConfigConfig.GRAY_400_80; - case SECONDARY_TRANSPARENT: - return new Color(0.9f, 0.9f, 0.9f, 0.2f).getRGB(); - case PRIMARY: - return OneConfigConfig.PRIMARY_700_80; - } - } - - switch (colorPalette) { - case SECONDARY_TRANSPARENT: // Formally -2 - return getColorComponents(color, new float[]{0f, 0f, 0f, 0f}, new float[]{0.9f, 0.9f, 0.9f, 0.3f}, hover, 50f); - case PRIMARY: // Formally 1 - return getColorComponents(color, splitColor(OneConfigConfig.PRIMARY_700), splitColor(OneConfigConfig.PRIMARY_600), hover, 20f); - default: - case SECONDARY: // Formally 0 - return getColorComponents(color, splitColor(OneConfigConfig.GRAY_500), splitColor(OneConfigConfig.GRAY_400), hover, 20f); - case TERTIARY: - return getColorComponents(color, splitColor(OneConfigConfig.WHITE_90), splitColor(OneConfigConfig.WHITE), hover, 20f); - case PRIMARY_DESTRUCTIVE: - return getColorComponents(color, splitColor(OneConfigConfig.ERROR_700), splitColor(OneConfigConfig.ERROR_600), hover, 20f); - case SECONDARY_DESTRUCTIVE: - return getColorComponents(color, splitColor(OneConfigConfig.ERROR_800), splitColor(OneConfigConfig.GRAY_500), !hover, 20f); - case TERTIARY_DESTRUCTIVE: - return getColorComponents(color, splitColor(OneConfigConfig.WHITE_90), splitColor(OneConfigConfig.ERROR_300), hover, 20f); - } - } - - /** - * Smooths the transition of a color between two colors. - * - * @param currentColor the current color (also the one you want to change) - * @param direction false to move towards initColor, true to move towards finalColor - * @param speed speed of the transition - */ - public static int smoothColor(int currentColor, int initColor, int finalColor, boolean direction, float speed) { - float[] init = splitColor(initColor); - float[] finalC = splitColor(finalColor); - float[] current = splitColor(currentColor); - return getColorComponents(current, init, finalC, direction, speed); - } - - private static float[] splitColor(int color) { - return new float[]{getRed(color) / 255f, getGreen(color) / 255f, getBlue(color) / 255f, getAlpha(color) / 255f}; - } - - private static int getColorComponents(float[] currentColor, float[] initColor, float[] finalColor, boolean hover, float speed) { - currentColor[0] = smooth(currentColor[0], initColor[0], finalColor[0], hover, speed); - currentColor[1] = smooth(currentColor[1], initColor[1], finalColor[1], hover, speed); - currentColor[2] = smooth(currentColor[2], initColor[2], finalColor[2], hover, speed); - currentColor[3] = smooth(currentColor[3], initColor[3], finalColor[3], hover, speed); - - return ((int) (currentColor[3] * 255) << 24) | - ((int) (currentColor[0] * 255) << 16) | - ((int) (currentColor[1] * 255) << 8) | - ((int) (currentColor[2] * 255)); - - } - - private static float smooth(float current, float min, float max, boolean moveToFinal, float speed) { - current = MathUtils.easeOut(current, moveToFinal ? max : min, speed); - if (current < min) { - current = min; - } - - if (current > max) { - current = max; - } - return current; - } - - /** - * Get the red component of an RGB color. - * - * @param color the color. - * @return the red component. - */ - public static int getRed(int color) { - return (color >> 16) & 0xFF; - } - - /** - * Get the green component of an RGB color. - * - * @param color the color. - * @return the green component. - */ - public static int getGreen(int color) { - return (color >> 8) & 0xFF; - } - - /** - * Get the blue component of an RGB color. - * - * @param color the color. - * @return the blue component. - */ - public static int getBlue(int color) { - return color & 0xFF; - } - - /** - * Get the alpha component of an ARGB color. - * - * @param color the color. - * @return the alpha component. - */ - public static int getAlpha(int color) { - return (color >> 24) & 0xFF; - } - - /** - * Get the RGB color from the given color components. - * - * @param red the red component. - * @param green the green component. - * @param blue the blue component. - * @param alpha the alpha component. - * @return the RGB color. - */ - public static int getColor(float red, float green, float blue, float alpha) { - return getColor((int) red * 255, (int) green * 255, (int) blue * 255, (int) alpha * 255); - } - - /** - * Get the RGB color from the given color components. - * - * @param red the red component. - * @param green the green component. - * @param blue the blue component. - * @return the RGB color. - */ - public static int getColor(int red, int green, int blue) { - return getColor(red, green, blue, 255); - } - - /** - * Get the RGB color from the given color components. - * - * @param red the red component. - * @param green the green component. - * @param blue the blue component. - * @param alpha the alpha component. - * @return the RGB color. - */ - public static int getColor(int red, int green, int blue, int alpha) { - return (alpha << 24) | (red << 16) | (green << 8) | blue; - } - - /** - * Return the color with the given red component. - * - * @param color the color. - * @param red the red component. - * @return the color with the given red component. - */ - public static int setRed(int color, int red) { - return (color & 0x00FFFF) | (red << 16); - } - - /** - * Return the color with the given green component. - * - * @param color the color. - * @param green the green component. - * @return the color with the given green component. - */ - public static int setGreen(int color, int green) { - return (color & 0xFF00FF) | (green << 8); - } - - /** - * Return the color with the given blue component. - * - * @param color the color. - * @param blue the blue component. - * @return the color with the given blue component. - */ - public static int setBlue(int color, int blue) { - return (color & 0xFFFF00) | blue; - } - - /** - * Return the color with the given alpha component. - * - * @param color the color. - * @param alpha the alpha component. - * @return the color with the given alpha component. - */ - public static int setAlpha(int color, int alpha) { - return (color & 0xFFFFFF) | (alpha << 24); - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java new file mode 100644 index 0000000..c2a1fec --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java @@ -0,0 +1,140 @@ +package cc.polyfrost.oneconfig.utils.color; + + +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.config.core.OneColor; + +import java.awt.*; + +import static cc.polyfrost.oneconfig.config.OneConfigConfig.*; + +public class ColorPalette { + /** + * Always returns transparent. + */ + public static final ColorPalette TRANSPARENT = new ColorPalette(OneConfigConfig.TRANSPARENT, OneConfigConfig.TRANSPARENT, OneConfigConfig.TRANSPARENT); + /** + *

Primary Color Scheme

Normal: Primary 600,
Hover: Primary 700,
Clicked: Primary 700 (80%) + */ + public static final ColorPalette PRIMARY = new ColorPalette(PRIMARY_600, PRIMARY_700, PRIMARY_700_80); + /** + *

Secondary Color Scheme

Normal: Gray 500,
Hover: Gray 400,
Clicked: Gray 400 (80%) + */ + public static final ColorPalette SECONDARY = new ColorPalette(OneConfigConfig.GRAY_500, OneConfigConfig.GRAY_400, OneConfigConfig.GRAY_400_80); + /** + *

Secondary (Transparent) Color Scheme

Normal: Transparent,
Hover: Gray rgba(229, 229, 229, 77),
Clicked: Gray rgba(229, 229, 229, 51) + */ + public static final ColorPalette SECONDARY_TRANSPARENT = new ColorPalette(OneConfigConfig.TRANSPARENT, new Color(229, 229, 229, 77).getRGB(), new Color(229, 229, 229, 51).getRGB()); + /** + *

Tertiary Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=White 100%),
Clicked: Transparent (Text=White 80%) + *

NOTICE this returns the text colors as it is always transparent.

+ */ + public static final ColorPalette TERTIARY = new ColorPalette(WHITE_90, WHITE, WHITE_80); + /** + *

Primary Destructive Color Scheme

Normal: Error 700,
Hover: Error 600,
Clicked: Error 600 (80%) + */ + public static final ColorPalette PRIMARY_DESTRUCTIVE = new ColorPalette(ERROR_700, ERROR_600, ERROR_600_80); + /** + *

Secondary Destructive Color Scheme

Normal: Gray 500,
Hover: Error 800,
Clicked: Error 800 (80%) + */ + public static final ColorPalette SECONDARY_DESTRUCTIVE = new ColorPalette(OneConfigConfig.GRAY_500, ERROR_800, ERROR_800_80); + /** + *

Tertiary Destructive Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=Error 300),
Clicked: Transparent (Text=Error 300 80%) + *

NOTICE this returns the text colors as it is always transparent.

+ */ + public static final ColorPalette TERTIARY_DESTRUCTIVE = new ColorPalette(WHITE_90, ERROR_300, ERROR_300_80); + + + + + private final int colorNormal; + private final int colorHovered; + private final int colorPressed; + private final float[] colorNormalf; + private final float[] colorHoveredf; + private final float[] colorPressedf; + + /**

Create a new ColorPalette.

+ * This color palette is used with animations, and the elements like BasicButton, BasicElement, and more. + *
This method takes integers in ARGB format, like many other classes, such as {@link OneColor} and {@link Color}. + * @param colorNormal the color of the element when it is not hovered or pressed. + * @param colorHovered the color of the element when it is hovered. + * @param colorPressed the color of the element when it is pressed. + */ + public ColorPalette(int colorNormal, int colorHovered, int colorPressed) { + this.colorNormal = colorNormal; + this.colorHovered = colorHovered; + this.colorPressed = colorPressed; + this.colorNormalf = new float[]{ColorUtils.getRed(colorNormal) / 255f, ColorUtils.getGreen(colorNormal) / 255f, ColorUtils.getBlue(colorNormal) / 255f, ColorUtils.getAlpha(colorNormal) / 255f}; + this.colorHoveredf = new float[]{ColorUtils.getRed(colorHovered) / 255f, ColorUtils.getGreen(colorHovered) / 255f, ColorUtils.getBlue(colorHovered) / 255f, ColorUtils.getAlpha(colorHovered) / 255f}; + this.colorPressedf = new float[]{ColorUtils.getRed(colorPressed) / 255f, ColorUtils.getGreen(colorPressed) / 255f, ColorUtils.getBlue(colorPressed) / 255f, ColorUtils.getAlpha(colorPressed) / 255f}; + } + /**

Create a new ColorPalette.

+ * This color palette is used with animations, and the elements like BasicButton, BasicElement, and more. + *
This method takes {@link OneColor} in ARGB format. + * @param colorNormal the color of the element when it is not hovered or pressed. + * @param colorHovered the color of the element when it is hovered. + * @param colorPressed the color of the element when it is pressed. + */ + public ColorPalette(OneColor colorNormal, OneColor colorHovered, OneColor colorPressed) { + this(colorNormal.getRGB(), colorHovered.getRGB(), colorPressed.getRGB()); + } + + /**

Create a new ColorPalette.

+ * This color palette is used with animations, and the elements like BasicButton, BasicElement, and more. + *
This method takes {@link Color} in ARGB format. + * @param colorNormal the color of the element when it is not hovered or pressed. + * @param colorHovered the color of the element when it is hovered. + * @param colorPressed the color of the element when it is pressed. + */ + public ColorPalette(Color colorNormal, Color colorHovered, Color colorPressed) { + this(colorNormal.getRGB(), colorHovered.getRGB(), colorPressed.getRGB()); + } + + /**

Create a new ColorPalette.

+ * This color palette is used with animations, and the elements like BasicButton, BasicElement, and more. + *
This method takes float arrays of the color between 0f and 1f, in [R, G, B, A] format. + * @param colorNormal the color of the element when it is not hovered or pressed. + * @param colorHovered the color of the element when it is hovered. + * @param colorPressed the color of the element when it is pressed. + */ + public ColorPalette(float[] colorNormal, float[] colorHovered, float[] colorPressed) { + this.colorNormalf = colorNormal; + this.colorHoveredf = colorHovered; + this.colorPressedf = colorPressed; + this.colorNormal = ColorUtils.getColor(colorNormal[0], colorNormal[1], colorNormal[2], colorNormal[3]); + this.colorHovered = ColorUtils.getColor(colorHovered[0], colorHovered[1], colorHovered[2], colorHovered[3]); + this.colorPressed = ColorUtils.getColor(colorPressed[0], colorPressed[1], colorPressed[2], colorPressed[3]); + } + + /** Return the color of the element when it is not hovered or pressed in ARGB format. */ + public int getNormalColor() { + return colorNormal; + } + + /** Return the color of the element when it is hovered in ARGB format. */ + public int getHoveredColor() { + return colorHovered; + } + + /** Return the color of the element when it is pressed in ARGB format. */ + public int getPressedColor() { + return colorPressed; + } + + /** Return the color of the element when it is not hovered or pressed in a float array (r,g,b,a). */ + public float[] getNormalColorf() { + return colorNormalf; + } + + /** Return the color of the element when it is hovered in a float array (r,g,b,a). */ + public float[] getHoveredColorf() { + return colorHoveredf; + } + + /** Return the color of the element when it is pressed in a float array (r,g,b,a). */ + public float[] getPressedColorf() { + return colorPressedf; + } + +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java new file mode 100644 index 0000000..a419766 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java @@ -0,0 +1,183 @@ +package cc.polyfrost.oneconfig.utils.color; + +import cc.polyfrost.oneconfig.utils.MathUtils; + +/** + * A class to help with color manipulation. + */ +public final class ColorUtils { + + public static int getColor(int currentColor, ColorPalette colorPalette, boolean hover, boolean click) { + float[] color = splitColor(currentColor); + if (click) { + return colorPalette.getPressedColor(); + } + + return getColorComponents(color, colorPalette.getNormalColorf(), colorPalette.getHoveredColorf(), hover, 20f); + } + + /** + * Smooths the transition of a color between two colors. + * + * @param currentColor the current color (also the one you want to change) + * @param direction false to move towards initColor, true to move towards finalColor + * @param speed speed of the transition + */ + public static int smoothColor(int currentColor, int initColor, int finalColor, boolean direction, float speed) { + float[] init = splitColor(initColor); + float[] finalC = splitColor(finalColor); + float[] current = splitColor(currentColor); + return getColorComponents(current, init, finalC, direction, speed); + } + + private static float[] splitColor(int color) { + return new float[]{getRed(color) / 255f, getGreen(color) / 255f, getBlue(color) / 255f, getAlpha(color) / 255f}; + } + + private static int getColorComponents(float[] currentColor, float[] initColor, float[] finalColor, boolean hover, float speed) { + currentColor[0] = smooth(currentColor[0], initColor[0], finalColor[0], hover, speed); + currentColor[1] = smooth(currentColor[1], initColor[1], finalColor[1], hover, speed); + currentColor[2] = smooth(currentColor[2], initColor[2], finalColor[2], hover, speed); + currentColor[3] = smooth(currentColor[3], initColor[3], finalColor[3], hover, speed); + + return ((int) (currentColor[3] * 255) << 24) | + ((int) (currentColor[0] * 255) << 16) | + ((int) (currentColor[1] * 255) << 8) | + ((int) (currentColor[2] * 255)); + + } + + private static float smooth(float current, float min, float max, boolean moveToFinal, float speed) { + current = MathUtils.easeOut(current, moveToFinal ? max : min, speed); + if (current < min) { + current = min; + } + + if (current > max) { + current = max; + } + return current; + } + + /** + * Get the red component of an RGB color. + * + * @param color the color. + * @return the red component. + */ + public static int getRed(int color) { + return (color >> 16) & 0xFF; + } + + /** + * Get the green component of an RGB color. + * + * @param color the color. + * @return the green component. + */ + public static int getGreen(int color) { + return (color >> 8) & 0xFF; + } + + /** + * Get the blue component of an RGB color. + * + * @param color the color. + * @return the blue component. + */ + public static int getBlue(int color) { + return color & 0xFF; + } + + /** + * Get the alpha component of an ARGB color. + * + * @param color the color. + * @return the alpha component. + */ + public static int getAlpha(int color) { + return (color >> 24) & 0xFF; + } + + /** + * Get the RGB color from the given color components. + * + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @param alpha the alpha component. + * @return the RGB color. + */ + public static int getColor(float red, float green, float blue, float alpha) { + return getColor((int) red * 255, (int) green * 255, (int) blue * 255, (int) alpha * 255); + } + + /** + * Get the RGB color from the given color components. + * + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @return the RGB color. + */ + public static int getColor(int red, int green, int blue) { + return getColor(red, green, blue, 255); + } + + /** + * Get the RGB color from the given color components. + * + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @param alpha the alpha component. + * @return the RGB color. + */ + public static int getColor(int red, int green, int blue, int alpha) { + return (alpha << 24) | (red << 16) | (green << 8) | blue; + } + + /** + * Return the color with the given red component. + * + * @param color the color. + * @param red the red component. + * @return the color with the given red component. + */ + public static int setRed(int color, int red) { + return (color & 0x00FFFF) | (red << 16); + } + + /** + * Return the color with the given green component. + * + * @param color the color. + * @param green the green component. + * @return the color with the given green component. + */ + public static int setGreen(int color, int green) { + return (color & 0xFF00FF) | (green << 8); + } + + /** + * Return the color with the given blue component. + * + * @param color the color. + * @param blue the blue component. + * @return the color with the given blue component. + */ + public static int setBlue(int color, int blue) { + return (color & 0xFFFF00) | blue; + } + + /** + * Return the color with the given alpha component. + * + * @param color the color. + * @param alpha the alpha component. + * @return the color with the given alpha component. + */ + public static int setAlpha(int color, int alpha) { + return (color & 0xFFFFFF) | (alpha << 24); + } +} -- cgit