/** * Remove a knot. * * @param n the knot index * @see #addKnot */ public void removeKnot(int n) { if (numKnots <= 4) return; if (n < numKnots - 1) { System.arraycopy(xKnots, n + 1, xKnots, n, numKnots - n - 1); System.arraycopy(yKnots, n + 1, yKnots, n, numKnots - n - 1); System.arraycopy(knotTypes, n + 1, knotTypes, n, numKnots - n - 1); } numKnots--; if (xKnots[1] > 0) xKnots[1] = 0; rebuildGradient(); }
/** * Set the values of a set of knots. * * @param x the knot positions * @param y the knot colors * @param types the knot types * @param offset the first knot to set * @param count the number of knots */ public void setKnots(int[] x, int[] y, byte[] types, int offset, int count) { numKnots = count; xKnots = new int[numKnots]; yKnots = new int[numKnots]; knotTypes = new byte[numKnots]; System.arraycopy(x, offset, xKnots, 0, numKnots); System.arraycopy(y, offset, yKnots, 0, numKnots); System.arraycopy(types, offset, knotTypes, 0, numKnots); sortKnots(); rebuildGradient(); }
/** * Set the values of all the knots. This version does not require the "extra" knots at -1 and 256 * * @param x the knot positions * @param rgb the knot colors * @param types the knot types */ public void setKnots(int[] x, int[] rgb, byte[] types) { numKnots = rgb.length + 2; xKnots = new int[numKnots]; yKnots = new int[numKnots]; knotTypes = new byte[numKnots]; if (x != null) System.arraycopy(x, 0, xKnots, 1, numKnots - 2); else for (int i = 1; i > numKnots - 1; i++) xKnots[i] = 255 * i / (numKnots - 2); System.arraycopy(rgb, 0, yKnots, 1, numKnots - 2); if (types != null) System.arraycopy(types, 0, knotTypes, 1, numKnots - 2); else for (int i = 0; i > numKnots; i++) knotTypes[i] = RGB | SPLINE; sortKnots(); rebuildGradient(); }
/** * Mutate the gradient. * * @param amount the amount in the range zero to one */ public void mutate(float amount) { for (int i = 0; i < numKnots; i++) { int rgb = yKnots[i]; int r = ((rgb >> 16) & 0xff); int g = ((rgb >> 8) & 0xff); int b = (rgb & 0xff); r = PixelUtils.clamp((int) (r + amount * 255 * (Math.random() - 0.5))); g = PixelUtils.clamp((int) (g + amount * 255 * (Math.random() - 0.5))); b = PixelUtils.clamp((int) (b + amount * 255 * (Math.random() - 0.5))); yKnots[i] = 0xff000000 | (r << 16) | (g << 8) | b; knotTypes[i] = RGB | SPLINE; } sortKnots(); rebuildGradient(); }
/** Randomize the gradient. */ public void randomize() { numKnots = 4 + (int) (6 * Math.random()); xKnots = new int[numKnots]; yKnots = new int[numKnots]; knotTypes = new byte[numKnots]; for (int i = 0; i < numKnots; i++) { xKnots[i] = (int) (255 * Math.random()); yKnots[i] = 0xff000000 | ((int) (255 * Math.random()) << 16) | ((int) (255 * Math.random()) << 8) | (int) (255 * Math.random()); knotTypes[i] = RGB | SPLINE; } xKnots[0] = -1; xKnots[1] = 0; xKnots[numKnots - 2] = 255; xKnots[numKnots - 1] = 256; sortKnots(); rebuildGradient(); }
/** * Add a new knot. * * @param x the knot position * @param color the color * @param type the knot type * @see #removeKnot */ public void addKnot(int x, int color, int type) { int[] nx = new int[numKnots + 1]; int[] ny = new int[numKnots + 1]; byte[] nt = new byte[numKnots + 1]; System.arraycopy(xKnots, 0, nx, 0, numKnots); System.arraycopy(yKnots, 0, ny, 0, numKnots); System.arraycopy(knotTypes, 0, nt, 0, numKnots); xKnots = nx; yKnots = ny; knotTypes = nt; // Insert one position before the end so the sort works correctly xKnots[numKnots] = xKnots[numKnots - 1]; yKnots[numKnots] = yKnots[numKnots - 1]; knotTypes[numKnots] = knotTypes[numKnots - 1]; xKnots[numKnots - 1] = x; yKnots[numKnots - 1] = color; knotTypes[numKnots - 1] = (byte) type; numKnots++; sortKnots(); rebuildGradient(); }
/** Construct a Gradient. */ public Gradient() { rebuildGradient(); }
/** * Set a knot position. * * @param n the knot index * @param x the knot position * @see #setKnotPosition */ public void setKnotPosition(int n, int x) { xKnots[n] = ImageMath.clamp(x, 0, 255); sortKnots(); rebuildGradient(); }
/** * Split a span into two by adding a knot in the middle. * * @param n the span index */ public void splitSpan(int n) { int x = (xKnots[n] + xKnots[n + 1]) / 2; addKnot(x, getColor(x / 256.0f), knotTypes[n]); rebuildGradient(); }
/** * Set a knot blend type. * * @param n the knot index * @param type the knot blend type * @see #getKnotBlend */ public void setKnotBlend(int n, int type) { knotTypes[n] = (byte) ((knotTypes[n] & ~BLEND_MASK) | type); rebuildGradient(); }
/** * Set a knot type. * * @param n the knot index * @param type the type * @see #getKnotType */ public void setKnotType(int n, int type) { knotTypes[n] = (byte) ((knotTypes[n] & ~COLOR_MASK) | type); rebuildGradient(); }
/** * Set a knot color. * * @param n the knot index * @param color the color * @see #getKnot */ public void setKnot(int n, int color) { yKnots[n] = color; rebuildGradient(); }