Commit fe552e0c authored by Nico Mack's avatar Nico Mack

Added MutableColor class

parent e6e0a8d2
......@@ -27,6 +27,12 @@ package lu.list.itis.dkd.tui.utility;
public class MathUtils {
// ***************************************************************************
// * Constants *
// ***************************************************************************
public static final int MAX_EIGHT_BITS = 255;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2018. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.tui.scripting.ScriptedProperty;
import com.google.common.base.Preconditions;
import java.awt.Color;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
* @version 1.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class MutableColor {
private Color colour;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final int HUE = 0;
private static final int SATURATION = 1;
private static final int BRIGHTNESS = 2;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param colour
*/
// ---------------------------------------------------------------------------
public MutableColor(Color colour) {
this.colour = (colour != null) ? colour : Color.BLACK;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param rgb
*/
// ---------------------------------------------------------------------------
@ScriptedProperty(property = "rgb")
public void setColor(int rgb) {
int alpha;
int rgba;
alpha = (this.colour != null) ? this.colour.getAlpha() : MathUtils.MAX_EIGHT_BITS;
if (alpha < MathUtils.MAX_EIGHT_BITS) {
rgba = rgb & 0x00FFFFFF;
rgba |= (alpha << 24);
this.colour = new Color(rgba, true);
} else {
this.colour = new Color(rgb, false);
}
}
// ---------------------------------------------------------------------------
/**
* @param alpha
*/
// ---------------------------------------------------------------------------
@ScriptedProperty(property = "alpha")
public void setAlpha(int alpha) {
int rgba = this.colour.getRGB();
rgba &= 0x00FFFFFF;
rgba |= (alpha << 24);
this.colour = new Color(rgba, true);
}
// ---------------------------------------------------------------------------
/**
* @param hue
*/
// ---------------------------------------------------------------------------
@ScriptedProperty(property = "hue")
public void setHue(int hue) {
float[] hsb = new float[3];
Preconditions.checkArgument(hue >= 0 && hue <= AngleUtils.THREE_SIXTY, "Hue(H) must be in the range 0 <= H <= 360!"); //$NON-NLS-1$
hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), hsb);
int alpha = colour.getAlpha();
hsb[HUE] *= hue / AngleUtils.THREE_SIXTY;
int rgb = Color.HSBtoRGB(hsb[HUE], hsb[SATURATION], hsb[BRIGHTNESS]);
rgb &= 0x00FFFFFF;
rgb |= (alpha << 24);
this.colour = new Color(rgb, true);
}
// ---------------------------------------------------------------------------
/**
* @param brightness
*/
// ---------------------------------------------------------------------------
@ScriptedProperty(property = "brightness")
public void setBrightness(int brightness) {
float[] hsb = new float[3];
Preconditions.checkArgument(brightness >= 0 && brightness <= 100, "Brightness(B) must be in the range 0 <= B <= 100!"); //$NON-NLS-1$
hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), hsb);
int alpha = colour.getAlpha();
hsb[BRIGHTNESS] *= brightness / 100f;
int rgb = Color.HSBtoRGB(hsb[HUE], hsb[SATURATION], hsb[BRIGHTNESS]);
rgb &= 0x00FFFFFF;
rgb |= (alpha << 24);
this.colour = new Color(rgb, true);
}
// ---------------------------------------------------------------------------
/**
* @param saturation
*/
// ---------------------------------------------------------------------------
@ScriptedProperty(property = "saturation")
public void setSaturation(int saturation) {
float[] hsb = new float[3];
Preconditions.checkArgument(saturation >= 0 && saturation <= 100, "Saturation(S) must be in the range 0 <= S <= 100!"); //$NON-NLS-1$
hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), hsb);
int alpha = colour.getAlpha();
hsb[SATURATION] *= saturation / 100f;
int rgb = Color.HSBtoRGB(hsb[HUE], hsb[SATURATION], hsb[BRIGHTNESS]);
rgb &= 0x00FFFFFF;
rgb |= (alpha << 24);
this.colour = new Color(rgb, true);
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public Color getColor() {
return this.colour;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
// ***************************************************************************
// ---------------------------------------------------------------------------
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment