/*
Copyright 2008-2010 Gephi
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
Website : http://www.gephi.org
This file is part of Gephi.
Gephi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Gephi 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
*/
package org.gephi.ui.utils;
import java.awt.Color;
/**
*
* @author Mathieu Bastian
*/
public class GradientUtils {
public static class LinearGradient {
private Color[] colors;
private float[] positions;
public LinearGradient(Color colors[], float[] positions) {
if (colors == null || positions == null) {
throw new NullPointerException();
}
if (colors.length != positions.length) {
throw new IllegalArgumentException();
}
this.colors = colors;
this.positions = positions;
}
public Color getValue(float pos) {
for (int a = 0; a < positions.length - 1; a++) {
if(positions[a]==pos) {
return colors[a];
}
if (positions[a] < pos && pos < positions[a + 1]) {
float v = (pos - positions[a]) / (positions[a + 1] - positions[a]);
return tween(colors[a], colors[a + 1], v);
}
}
if (pos <= positions[0]) {
return colors[0];
}
if (pos >= positions[positions.length - 1]) {
return colors[colors.length - 1];
}
return null;
}
private Color tween(Color c1, Color c2, float p) {
return new Color(
(int) (c1.getRed() * (1 - p) + c2.getRed() * (p)),
(int) (c1.getGreen() * (1 - p) + c2.getGreen() * (p)),
(int) (c1.getBlue() * (1 - p) + c2.getBlue() * (p)),
(int) (c1.getAlpha() * (1 - p) + c2.getAlpha() * (p)));
}
public Color[] getColors() {
return colors;
}
public float[] getPositions() {
return positions;
}
public void setColors(Color[] colors) {
this.colors = colors;
}
public void setPositions(float[] positions) {
this.positions = positions;
}
}
}