rgb -> argb

This commit is contained in:
Astrash 2023-05-02 13:04:23 +10:00
parent 3c593c7013
commit dacddef5d6
2 changed files with 62 additions and 62 deletions

View File

@ -70,7 +70,7 @@ public class ColorLoader implements TypeLoader<ColorString> {
green = Integer.parseInt(hex.substring(2, 4), 16); green = Integer.parseInt(hex.substring(2, 4), 16);
blue = Integer.parseInt(hex.substring(4, 6), 16); blue = Integer.parseInt(hex.substring(4, 6), 16);
return ColorUtil.rgbValidated(alpha, red, green, blue); return ColorUtil.argbValidated(alpha, red, green, blue);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IllegalArgumentException("Failed to parse hex color", e); throw new IllegalArgumentException("Failed to parse hex color", e);
} }
@ -83,7 +83,7 @@ public class ColorLoader implements TypeLoader<ColorString> {
int g = Integer.decode(green); int g = Integer.decode(green);
int b = Integer.decode(blue); int b = Integer.decode(blue);
return ColorUtil.rgbValidated(a, r, g, b); return ColorUtil.argbValidated(a, r, g, b);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid channel value", e); throw new IllegalArgumentException("Invalid channel value", e);
} }

View File

@ -13,145 +13,145 @@ public class ColorUtil {
FastMath.abs(getBlue(a) - getBlue(b)); FastMath.abs(getBlue(a) - getBlue(b));
} }
public static int getRed(int rgb) { public static int getRed(int argb) {
return rgb >> 16 & 255; return argb >> 16 & 255;
} }
public static int getGreen(int rgb) { public static int getGreen(int argb) {
return rgb >> 8 & 255; return argb >> 8 & 255;
} }
public static int getBlue(int rgb) { public static int getBlue(int argb) {
return rgb >> 0 & 255; return argb >> 0 & 255;
} }
public static int getAlpha(int rgb) { public static int getAlpha(int argb) {
return rgb >> 24 & 255; return argb >> 24 & 255;
} }
public static int getGrayscale(int rgb) { public static int getGrayscale(int argb) {
return (getRed(rgb) + getGreen(rgb) + getBlue(rgb)) / 3; return (getRed(argb) + getGreen(argb) + getBlue(argb)) / 3;
} }
public static int getChannel(int rgb, Channel channel) { public static int getChannel(int argb, Channel channel) {
return channel.from(rgb); return channel.from(argb);
} }
public static int zeroRed(int rgb) { public static int zeroRed(int argb) {
return rgb & ~0x00FF0000; return argb & ~0x00FF0000;
} }
public static int zeroGreen(int rgb) { public static int zeroGreen(int argb) {
return rgb & ~0x0000FF00; return argb & ~0x0000FF00;
} }
public static int zeroBlue(int rgb) { public static int zeroBlue(int argb) {
return rgb & ~0x000000FF; return argb & ~0x000000FF;
} }
public static int zeroAlpha(int rgb) { public static int zeroAlpha(int argb) {
return rgb & ~0xFF000000; return argb & ~0xFF000000;
} }
public static int zeroGrayscale(int rgb) { public static int zeroGrayscale(int argb) {
return rgb & ~0x00FFFFFF; return argb & ~0x00FFFFFF;
} }
public static int zeroChannel(int rgb, Channel channel) { public static int zeroChannel(int argb, Channel channel) {
return channel.zero(rgb); return channel.zero(argb);
} }
/* /*
* Multiply each color channel by the alpha channel * Multiply each color channel by the alpha channel
*/ */
public static int premultiply(int rgb) { public static int premultiply(int argb) {
int alpha = getAlpha(rgb); int alpha = getAlpha(argb);
int red = (getRed(rgb) * alpha + 127) / 255; int red = (getRed(argb) * alpha + 127) / 255;
int green = (getGreen(rgb) * alpha + 127) / 255; int green = (getGreen(argb) * alpha + 127) / 255;
int blue = (getBlue(rgb) * alpha + 127) / 255; int blue = (getBlue(argb) * alpha + 127) / 255;
return rgb(alpha, red, green, blue); return argb(alpha, red, green, blue);
} }
public static int rgb(int alpha, int red, int green, int blue) { public static int argb(int alpha, int red, int green, int blue) {
return rgbAlpha(alpha) | rgbRed(red) | rgbGreen(green) | rgbBlue(blue); return argbAlpha(alpha) | argbRed(red) | argbGreen(green) | argbBlue(blue);
} }
public static int rgbValidated(int alpha, int red, int green, int blue) throws IllegalArgumentException { public static int argbValidated(int alpha, int red, int green, int blue) throws IllegalArgumentException {
if (alpha < 0 || alpha > 255 || if (alpha < 0 || alpha > 255 ||
red < 0 || red > 255 || red < 0 || red > 255 ||
green < 0 || green > 255 || green < 0 || green > 255 ||
blue < 0 || blue > 255 blue < 0 || blue > 255
) throw new IllegalArgumentException("Channel values must be in range 0-255"); ) throw new IllegalArgumentException("Channel values must be in range 0-255");
return rgb(alpha, red, green, blue); return argb(alpha, red, green, blue);
} }
public static int rgbAlpha(int alpha) { return alpha << 24; } public static int argbAlpha(int alpha) { return alpha << 24; }
public static int rgbRed(int red) { return red << 16; } public static int argbRed(int red) { return red << 16; }
public static int rgbGreen(int green) { return green << 8; } public static int argbGreen(int green) { return green << 8; }
public static int rgbBlue(int blue) { return blue; } public static int argbBlue(int blue) { return blue; }
public enum Channel { public enum Channel {
RED { RED {
@Override @Override
public int from(int rgb) { public int from(int argb) {
return getRed(rgb); return getRed(argb);
} }
@Override @Override
public int zero(int rgb) { public int zero(int argb) {
return zeroRed(rgb); return zeroRed(argb);
} }
}, },
GREEN { GREEN {
@Override @Override
public int from(int rgb) { public int from(int argb) {
return getGreen(rgb); return getGreen(argb);
} }
@Override @Override
public int zero(int rgb) { public int zero(int argb) {
return zeroGreen(rgb); return zeroGreen(argb);
} }
}, },
BLUE { BLUE {
@Override @Override
public int from(int rgb) { public int from(int argb) {
return getBlue(rgb); return getBlue(argb);
} }
@Override @Override
public int zero(int rgb) { public int zero(int argb) {
return zeroBlue(rgb); return zeroBlue(argb);
} }
}, },
GRAYSCALE { GRAYSCALE {
@Override @Override
public int from(int rgb) { public int from(int argb) {
return getGrayscale(rgb); return getGrayscale(argb);
} }
@Override @Override
public int zero(int rgb) { public int zero(int argb) {
return zeroGrayscale(rgb); return zeroGrayscale(argb);
} }
}, },
ALPHA { ALPHA {
@Override @Override
public int from(int rgb) { public int from(int argb) {
return getAlpha(rgb); return getAlpha(argb);
} }
@Override @Override
public int zero(int rgb) { public int zero(int argb) {
return zeroAlpha(rgb); return zeroAlpha(argb);
} }
}; };
public abstract int from(int rgb); public abstract int from(int argb);
public abstract int zero(int rgb); public abstract int zero(int argb);
} }
} }