Path: blob/master/SLICK_HOME/src/org/newdawn/slick/fills/GradientFill.java
1459 views
package org.newdawn.slick.fills;12import org.newdawn.slick.Color;3import org.newdawn.slick.ShapeFill;4import org.newdawn.slick.geom.Shape;5import org.newdawn.slick.geom.Vector2f;67/**8* A fill effect used to define gradients when filling and drawing shapes. A gradient is defined9* by two control points. Each point that is rendered is coloured based on it's proximity to the10* points. Note that the points are defined relative to the center of the shape being drawn. This11* is with the intention that the gradient fills can be used and do not need to be updated when12* the geometry is moved13*14* @author kevin15*/16public class GradientFill implements ShapeFill {17/** The contant offset */18private Vector2f none = new Vector2f(0,0);19/** The start position of the gradient */20private Vector2f start;21/** The end poisition of the gradient */22private Vector2f end;23/** The starting colour of the gradient */24private Color startCol;25/** The ending colour of the gradient */26private Color endCol;27/** True if the graident is defined in shape coordinates */28private boolean local = false;2930/**31* Create a gradient fill32*33* @param sx The x coordinate of the starting control point34* @param sy The y coordinate of the starting control point35* @param startCol The colour to apply at the starting control point36* @param ex The x coordinate of the ending control point37* @param ey The y coordinate of the ending control point38* @param endCol The colour to apply at the ending control point39*/40public GradientFill(float sx, float sy, Color startCol, float ex, float ey, Color endCol)41{42this(sx,sy,startCol,ex,ey,endCol,false);43}4445/**46* Create a gradient fill47*48* @param sx The x coordinate of the starting control point49* @param sy The y coordinate of the starting control point50* @param startCol The colour to apply at the starting control point51* @param ex The x coordinate of the ending control point52* @param ey The y coordinate of the ending control point53* @param endCol The colour to apply at the ending control point54* @param local True if the gradient is defined in local shape coordinates55*/56public GradientFill(float sx, float sy, Color startCol, float ex, float ey, Color endCol, boolean local)57{58this(new Vector2f(sx,sy), startCol, new Vector2f(ex,ey), endCol, local);59}6061/**62* Create a gradient fill63*64* @param start The position of the starting control point65* @param startCol The colour to apply at the starting control point66* @param end The position of the ending control point67* @param endCol The colour to apply at the ending control point68* @param local True if the gradient is defined in local shape coordinates69*/70public GradientFill(Vector2f start, Color startCol, Vector2f end, Color endCol, boolean local) {71this.start = new Vector2f(start);72this.end = new Vector2f(end);73this.startCol = new Color(startCol);74this.endCol = new Color(endCol);75this.local = local;76}7778/**79* Get an inverted copy of the gradient80*81* @return The copy with the colours inverted82*/83public GradientFill getInvertedCopy() {84return new GradientFill(start, endCol, end, startCol, local);85}8687/**88* Indicate if the gradient is defined in shape local coordinates89*90* @param local True if the gradient is defined in shape local coordinates91*/92public void setLocal(boolean local) {93this.local = local;94}9596/**97* Get the position of the start control point98*99* @return The position of the start control point100*/101public Vector2f getStart() {102return start;103}104105/**106* Get the position of the end control point107*108* @return The position of the end control point109*/110public Vector2f getEnd() {111return end;112}113114/**115* Get the colour at the start control point116*117* @return The color at the start control point118*/119public Color getStartColor() {120return startCol;121}122123/**124* Get the colour at the end control point125*126* @return The color at the end control point127*/128public Color getEndColor() {129return endCol;130}131132/**133* Set the start point's position134*135* @param x The x coordinate of the start control point136* @param y The y coordinate of the start control point137*/138public void setStart(float x, float y) {139setStart(new Vector2f(x,y));140}141142/**143* Set the start control point's position144*145* @param start The new poisition for the start point146*/147public void setStart(Vector2f start) {148this.start = new Vector2f(start);149}150151/**152* Set the end control point's position153*154* @param x The x coordinate of the end control point155* @param y The y coordinate of the end control point156*/157public void setEnd(float x, float y) {158setEnd(new Vector2f(x,y));159}160161/**162* Set the end control point's position163*164* @param end The new position for the end point165*/166public void setEnd(Vector2f end) {167this.end = new Vector2f(end);168}169170/**171* Set the colour to apply at the start control's position172*173* @param color The colour to apply at the start control point174*/175public void setStartColor(Color color) {176this.startCol = new Color(color);177}178179/**180* Set the colour to apply at the end control's position181*182* @param color The colour to apply at the end control point183*/184public void setEndColor(Color color) {185this.endCol = new Color(color);186}187188/**189* Get the colour that should be applied at the specified location190*191* @param shape The shape being filled192* @param x The x coordinate of the point being coloured193* @param y The y coordinate of the point being coloured194* @return The colour that should be applied based on the control points of this gradient195*/196public Color colorAt(Shape shape, float x, float y) {197if (local) {198return colorAt(x-shape.getCenterX(),y-shape.getCenterY());199} else {200return colorAt(x,y);201}202}203204/**205* Get the colour that should be applied at the specified location206*207* @param x The x coordinate of the point being coloured208* @param y The y coordinate of the point being coloured209* @return The colour that should be applied based on the control points of this gradient210*/211public Color colorAt(float x, float y) {212float dx1 = end.getX() - start.getX();213float dy1 = end.getY() - start.getY();214215float dx2 = -dy1;216float dy2 = dx1;217float denom = (dy2 * dx1) - (dx2 * dy1);218219if (denom == 0) {220return Color.black;221}222223float ua = (dx2 * (start.getY() - y)) - (dy2 * (start.getX() - x));224ua /= denom;225float ub = (dx1 * (start.getY() - y)) - (dy1 * (start.getX() - x));226ub /= denom;227float u = ua;228if (u < 0) {229u = 0;230}231if (u > 1) {232u = 1;233}234float v = 1 - u;235236// u is the proportion down the line we are237Color col = new Color(1,1,1,1);238col.r = (u * endCol.r) + (v * startCol.r);239col.b = (u * endCol.b) + (v * startCol.b);240col.g = (u * endCol.g) + (v * startCol.g);241col.a = (u * endCol.a) + (v * startCol.a);242243return col;244}245246/**247* @see org.newdawn.slick.ShapeFill#getOffsetAt(org.newdawn.slick.geom.Shape, float, float)248*/249public Vector2f getOffsetAt(Shape shape, float x, float y) {250return none;251}252}253254255