cleanup: Color
This commit is contained in:
parent
45b851c55c
commit
f8603dafe1
@ -14,78 +14,101 @@
|
|||||||
* You should have received a copy of the GNU Lesser General Public License along with
|
* You should have received a copy of the GNU Lesser General Public License along with
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
* Copyright (C) 2006 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.oscim.backend.canvas;
|
package org.oscim.backend.canvas;
|
||||||
|
|
||||||
import org.oscim.utils.FastMath;
|
import org.oscim.utils.FastMath;
|
||||||
|
|
||||||
/**
|
|
||||||
* The Class Color.
|
|
||||||
*/
|
|
||||||
public class Color {
|
public class Color {
|
||||||
|
|
||||||
/** The Constant BLACK. */
|
public static int fade(int color, double alpha) {
|
||||||
public static final int BLACK = 0xFF000000;
|
alpha = FastMath.clamp(alpha, 0, 1);
|
||||||
|
|
||||||
/** The Constant DKGRAY. */
|
alpha *= (color >>> 24) & 0xff;
|
||||||
public static final int DKGRAY = 0xFF444444;
|
int c = (((int) alpha) & 0xff) << 24;
|
||||||
|
|
||||||
/** The Constant GRAY. */
|
alpha /= 255;
|
||||||
public static final int GRAY = 0xFF888888;
|
|
||||||
|
|
||||||
/** The Constant LTGRAY. */
|
c |= ((int) (alpha * ((color >>> 16) & 0xff))) << 16;
|
||||||
public static final int LTGRAY = 0xFFCCCCCC;
|
c |= ((int) (alpha * ((color >>> 8) & 0xff))) << 8;
|
||||||
|
c |= ((int) (alpha * (color & 0xff)));
|
||||||
|
|
||||||
/** The Constant WHITE. */
|
return c;
|
||||||
public static final int WHITE = 0xFFFFFFFF;
|
}
|
||||||
|
|
||||||
/** The Constant RED. */
|
public static int rainbow(float pos) {
|
||||||
public static final int RED = 0xFFFF0000;
|
float i = 255 * pos;
|
||||||
|
int r = (int) Math.round(Math.sin(0.024 * i + 0) * 127 + 128);
|
||||||
/** The Constant GREEN. */
|
int g = (int) Math.round(Math.sin(0.024 * i + 2) * 127 + 128);
|
||||||
public static final int GREEN = 0xFF00FF00;
|
int b = (int) Math.round(Math.sin(0.024 * i + 4) * 127 + 128);
|
||||||
|
return 0xff000000 | (r << 16) | (g << 8) | b;
|
||||||
/** The Constant BLUE. */
|
}
|
||||||
public static final int BLUE = 0xFF0000FF;
|
|
||||||
|
|
||||||
/** The Constant YELLOW. */
|
|
||||||
public static final int YELLOW = 0xFFFFFF00;
|
|
||||||
|
|
||||||
/** The Constant CYAN. */
|
|
||||||
public static final int CYAN = 0xFF00FFFF;
|
|
||||||
|
|
||||||
/** The Constant MAGENTA. */
|
|
||||||
public static final int MAGENTA = 0xFFFF00FF;
|
|
||||||
|
|
||||||
/** The Constant TRANSPARENT. */
|
|
||||||
public static final int TRANSPARENT = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pack 8 bit r, g, b into one int.
|
* Pack r, g, b bytes into one int.
|
||||||
*
|
|
||||||
* @param r the r
|
|
||||||
* @param g the g
|
|
||||||
* @param b the b
|
|
||||||
* @return the int
|
|
||||||
*/
|
*/
|
||||||
public static int get(int r, int g, int b) {
|
public static int get(int r, int g, int b) {
|
||||||
return 0xff << 24 | r << 16 | g << 8 | b;
|
return 0xff << 24 | r << 16 | g << 8 | b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pack premultiplied a, r, g, b bytes into one int.
|
||||||
|
*/
|
||||||
|
public static int get(int a, int r, int g, int b) {
|
||||||
|
return a << 24 | r << 16 | g << 8 | b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pack r, g, b bytes into one int with premultiplied alpha a.
|
||||||
|
*/
|
||||||
|
public static int get(float a, int r, int g, int b) {
|
||||||
|
return fade(0xff << 24 | r << 16 | g << 8 | b, a);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float rToFloat(int color) {
|
||||||
|
return ((color >>> 16) & 0xff) / 255f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float gToFloat(int color) {
|
||||||
|
return ((color >>> 8) & 0xff) / 255f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float bToFloat(int color) {
|
||||||
|
return ((color) & 0xff) / 255f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float aToFloat(int color) {
|
||||||
|
return ((color >>> 24) & 0xff) / 255f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2006 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
public static final int BLACK = 0xFF000000;
|
||||||
|
public static final int DKGRAY = 0xFF444444;
|
||||||
|
public static final int GRAY = 0xFF888888;
|
||||||
|
public static final int LTGRAY = 0xFFCCCCCC;
|
||||||
|
public static final int WHITE = 0xFFFFFFFF;
|
||||||
|
public static final int RED = 0xFFFF0000;
|
||||||
|
public static final int GREEN = 0xFF00FF00;
|
||||||
|
public static final int BLUE = 0xFF0000FF;
|
||||||
|
public static final int YELLOW = 0xFFFFFF00;
|
||||||
|
public static final int CYAN = 0xFF00FFFF;
|
||||||
|
public static final int MAGENTA = 0xFFFF00FF;
|
||||||
|
public static final int TRANSPARENT = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the color string, and return the corresponding color-int.
|
* Parse the color string, and return the corresponding color-int.
|
||||||
* If the string cannot be parsed, throws an IllegalArgumentException
|
* If the string cannot be parsed, throws an IllegalArgumentException
|
||||||
@ -112,43 +135,4 @@ public class Color {
|
|||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Unknown color");
|
throw new IllegalArgumentException("Unknown color");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int fade(int color, double alpha) {
|
|
||||||
alpha = FastMath.clamp(alpha, 0, 1);
|
|
||||||
|
|
||||||
alpha *= (color >>> 24) & 0xff;
|
|
||||||
int c = (((int) alpha) & 0xff) << 24;
|
|
||||||
|
|
||||||
alpha /= 255;
|
|
||||||
|
|
||||||
c |= ((int) (alpha * ((color >>> 16) & 0xff))) << 16;
|
|
||||||
c |= ((int) (alpha * ((color >>> 8) & 0xff))) << 8;
|
|
||||||
c |= ((int) (alpha * (color & 0xff)));
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float rToFloat(int color) {
|
|
||||||
return ((color >>> 16) & 0xff) / 255f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float gToFloat(int color) {
|
|
||||||
return ((color >>> 8) & 0xff) / 255f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float bToFloat(int color) {
|
|
||||||
return ((color) & 0xff) / 255f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float aToFloat(int color) {
|
|
||||||
return ((color >>> 24) & 0xff) / 255f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int rainbow(float pos) {
|
|
||||||
float i = 255 * pos;
|
|
||||||
int r = (int) Math.round(Math.sin(0.024 * i + 0) * 127 + 128);
|
|
||||||
int g = (int) Math.round(Math.sin(0.024 * i + 2) * 127 + 128);
|
|
||||||
int b = (int) Math.round(Math.sin(0.024 * i + 4) * 127 + 128);
|
|
||||||
return 0xff000000 | (r << 16) | (g << 8) | b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user