• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2011, Novyon Events
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @author Anthyon
29  */
30 package com.jme3.terrain.noise;
31 
32 /**
33  * Helper class for working with colors and gradients
34  *
35  * @author Anthyon
36  *
37  */
38 public class Color {
39 
40 	private final float[] rgba = new float[4];
41 
Color()42 	public Color() {}
43 
Color(final int r, final int g, final int b)44 	public Color(final int r, final int g, final int b) {
45 		this(r, g, b, 255);
46 	}
47 
Color(final int r, final int g, final int b, final int a)48 	public Color(final int r, final int g, final int b, final int a) {
49 		this.rgba[0] = (r & 255) / 256f;
50 		this.rgba[1] = (g & 255) / 256f;
51 		this.rgba[2] = (b & 255) / 256f;
52 		this.rgba[3] = (a & 255) / 256f;
53 	}
54 
Color(final float r, final float g, final float b)55 	public Color(final float r, final float g, final float b) {
56 		this(r, g, b, 1);
57 	}
58 
Color(final float r, final float g, final float b, final float a)59 	public Color(final float r, final float g, final float b, final float a) {
60 		this.rgba[0] = ShaderUtils.clamp(r, 0, 1);
61 		this.rgba[1] = ShaderUtils.clamp(g, 0, 1);
62 		this.rgba[2] = ShaderUtils.clamp(b, 0, 1);
63 		this.rgba[3] = ShaderUtils.clamp(a, 0, 1);
64 	}
65 
Color(final int h, final float s, final float b)66 	public Color(final int h, final float s, final float b) {
67 		this(h, s, b, 1);
68 	}
69 
Color(final int h, final float s, final float b, final float a)70 	public Color(final int h, final float s, final float b, final float a) {
71 		this.rgba[3] = a;
72 		if (s == 0) {
73 			// achromatic ( grey )
74 			this.rgba[0] = b;
75 			this.rgba[1] = b;
76 			this.rgba[2] = b;
77 			return;
78 		}
79 
80 		float hh = h / 60.0f;
81 		int i = ShaderUtils.floor(hh);
82 		float f = hh - i;
83 		float p = b * (1 - s);
84 		float q = b * (1 - s * f);
85 		float t = b * (1 - s * (1 - f));
86 
87 		if (i == 0) {
88 			this.rgba[0] = b;
89 			this.rgba[1] = t;
90 			this.rgba[2] = p;
91 		} else if (i == 1) {
92 			this.rgba[0] = q;
93 			this.rgba[1] = b;
94 			this.rgba[2] = p;
95 		} else if (i == 2) {
96 			this.rgba[0] = p;
97 			this.rgba[1] = b;
98 			this.rgba[2] = t;
99 		} else if (i == 3) {
100 			this.rgba[0] = p;
101 			this.rgba[1] = q;
102 			this.rgba[2] = b;
103 		} else if (i == 4) {
104 			this.rgba[0] = t;
105 			this.rgba[1] = p;
106 			this.rgba[2] = b;
107 		} else {
108 			this.rgba[0] = b;
109 			this.rgba[1] = p;
110 			this.rgba[2] = q;
111 		}
112 	}
113 
toInteger()114 	public int toInteger() {
115 		return 0x00000000 | (int) (this.rgba[3] * 256) << 24 | (int) (this.rgba[0] * 256) << 16 | (int) (this.rgba[1] * 256) << 8
116 				| (int) (this.rgba[2] * 256);
117 	}
118 
toWeb()119 	public String toWeb() {
120 		return Integer.toHexString(this.toInteger());
121 	}
122 
toGrayscale()123 	public Color toGrayscale() {
124 		float v = (this.rgba[0] + this.rgba[1] + this.rgba[2]) / 3f;
125 		return new Color(v, v, v, this.rgba[3]);
126 	}
127 
toSepia()128 	public Color toSepia() {
129 		float r = ShaderUtils.clamp(this.rgba[0] * 0.393f + this.rgba[1] * 0.769f + this.rgba[2] * 0.189f, 0, 1);
130 		float g = ShaderUtils.clamp(this.rgba[0] * 0.349f + this.rgba[1] * 0.686f + this.rgba[2] * 0.168f, 0, 1);
131 		float b = ShaderUtils.clamp(this.rgba[0] * 0.272f + this.rgba[1] * 0.534f + this.rgba[2] * 0.131f, 0, 1);
132 		return new Color(r, g, b, this.rgba[3]);
133 	}
134 }
135