summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ColorUtil.java
blob: d701313f4c2ba87cdf2e4bcd647553d6d4d468e9 (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
60
61
62
63
64
65
66
67
68
69
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) {
		float a[] = new float[3];
		color.getRGBColorComponents(a);
		return a[0] * 0.2126f + a[1] * 0.7152f + a[2] * 0.0722f;
	}
	
	public static float getContrast(Color a, Color b) {
		float val = (getBrightness(a) + 0.05f) / (getBrightness(b) + 0.05f);
		if (val < 1) {
			val = 1 / val;
		}
		return val;
	}

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