aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java146
1 files changed, 143 insertions, 3 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java
index ec40500..3dbbe39 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java
@@ -173,10 +173,104 @@ public interface Color {
/** HSL Luma, from 0..1 */
private float luma;
+ /**
+ *
+ * @param hue hue angle, between 0 and 1
+ * @param sat saturation, between 0 and 1
+ * @param luma luminance, between 0 and 1
+ */
+ public HSL(float hue, float sat, float luma) {
+ this.hue = hue;
+ this.sat = sat;
+ this.luma = luma;
+ }
+
+ public HSL(int rgb) {
+ float r = i_f(rgb >> 16);
+ float g = i_f(rgb >> 8);
+ float b = i_f(rgb);
+
+ float max = Math.max(r, Math.max(g, b));
+ float min = Math.min(r, Math.min(g, b));
+
+ hue = 0;
+ if (max==min) {
+ hue = 0;
+ } else if (max==r) {
+ hue = (g-b)/(max-min);
+ hue *= 60f/360f;
+ } else if (max==g) {
+ hue = 2 + (b-r)/(max-min);
+ hue *= 60f/360f;
+ } else { //max==b
+ hue = 4 + (r-g)/(max-min);
+ hue *= 60f/360f;
+ }
+ if (hue<0) hue+=1;
+
+ luma = (max+min)/2f;
+
+ sat = 0;
+ if (max==0) {
+ // the saturation of black is zero, special-cased because (0-0)/0 is undefined
+ } else if (min==1) {
+ // the saturation of white is zero: (1-1)/1, special-cased for kicks I guess
+ } else {
+ sat = (max-luma) / Math.min(luma, 1-luma);
+ }
+ }
+
public int toRgb() {
- float chroma = 1 - (Math.abs(2*luma - 1));
- //TODO: Finish implementing
- return 0;
+ float chroma = (1 - (Math.abs(2*luma - 1))) * sat;
+
+ int h = (int)(hue*6); h %= 6;
+
+ float x = 1f-Math.abs((hue*6 % 2)-1f); x *= chroma;
+ float m = luma - (chroma/2);
+
+ while (h<0) h+=6;
+ if (h>=6) h = h % 6;
+ float rf = 0;
+ float gf = 0;
+ float bf = 0;
+
+ switch(h) {
+ case 0:
+ rf = chroma+m;
+ gf = x+m;
+ bf = 0+m;
+ break;
+ case 1:
+ rf = x+m;
+ gf = chroma+m;
+ bf = 0+m;
+ break;
+ case 2:
+ rf = 0+m;
+ gf = chroma+m;
+ bf = x+m;
+ break;
+ case 3:
+ rf = 0+m;
+ gf = x+m;
+ bf = chroma+m;
+ break;
+ case 4:
+ rf = x+m;
+ gf = 0+m;
+ bf = chroma+m;
+ break;
+ case 5:
+ rf = chroma+m;
+ gf = 0+m;
+ bf = x+m;
+ }
+
+ int r = f_255(rf);
+ int g = f_255(gf);
+ int b = f_255(bf);
+
+ return 0xFF_000000 | (r << 16) | (g << 8) | b;
}
public float getHue() {
@@ -190,5 +284,51 @@ public interface Color {
public float getLuma() {
return luma;
}
+
+ private static int f_255(float f) {
+ int result = (int)(f*255);
+ return Math.min(255,Math.max(0, result));
+ }
+
+ private static float i_f(int i) {
+ return (i & 0xFF) / 255f;
+ }
+ }
+
+ public static class LCH implements Color {
+ /** HCL Luma, from 0..1 */
+ private float luma;
+ /** HCL Chroma, from 0..1 */
+ private float chroma;
+ /** HCL Hue, from 0..1 */
+ private float hue;
+
+
+
+ public LCH(float luma, float chroma, float hue) {
+ this.luma = luma;
+ this.chroma = chroma;
+ this.hue = hue;
+ }
+
+ @Override
+ public int toRgb() {
+
+
+ //Was going to steal Grondag code but then I looked at it
+ return 0; //TODO: Implement
+ }
+
+ public float getHue() {
+ return hue;
+ }
+
+ public float getChroma() {
+ return chroma;
+ }
+
+ public float getLuma() {
+ return luma;
+ }
}
}