summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ColorUtil.java
blob: f086712ea78748b12a9004e3a93327f21f397ea0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.openslx.dozmod.gui.helper;

import java.awt.Color;

public class ColorUtil {

	private static float[] squared(Color color) {
		float[] vals = color.getRGBComponents(null);
		vals[0] *= vals[0];
		vals[1] *= vals[1];
		vals[2] *= vals[2];
		return vals;
	}

	private static Color sqrt(float... floatVals) {
		floatVals[0] = (float) Math.sqrt(floatVals[0]);
		floatVals[1] = (float) Math.sqrt(floatVals[1]);
		floatVals[2] = (float) Math.sqrt(floatVals[2]);
		return new Color(floatVals[0], floatVals[1], floatVals[2]);
	}

	public static Color blendFast(Color color1, Color color2, float wc1) {
		float[] c1 = color1.getColorComponents(null);
		float[] c2 = color2.getColorComponents(null);
		float wc2 = 1f - wc1;
		return new Color(c1[0] * wc1 + c2[0] * wc2, c1[1] * wc1 + c2[1] * wc2, c1[2] * wc1 + c2[2] * wc2);
	}

	public static Color blend(Color color1, Color color2, float wc1) {
		float[] c1 = squared(color1);
		float[] c2 = squared(color2);
		float wc2 = 1f - wc1;
		return sqrt(c1[0] * wc1 + c2[0] * wc2, c1[1] * wc1 + c2[1] * wc2, c1[2] * wc1 + c2[2] * wc2);
	}

	public static Color add(Color color1, Color color2) {
		float[] c1 = squared(color1);
		float[] c2 = squared(color2);
		c1[0] += c2[0];
		c1[1] += c2[1];
		c1[2] += c2[2];
		float max = Math.max(Math.max(c1[0], c1[1]), c1[2]);
		if (max > 1f) {
			c1[0] /= max;
			c1[1] /= max;
			c1[2] /= max;
		}
		return sqrt(c1[0], c1[1], c1[2]);
	}

	public static float getBrightness(Color color) {
		return Color.RGBtoHSB(color.getRed(), color.getBlue(), color.getGreen(), null)[2];
	}

	public static Color contrastColor(Color color) {
		return new Color(color.getRed() ^ 0x80, color.getGreen() ^ 0x80, color.getBlue() ^ 0x80);
	}

}