Class PeasyGradients


  • public final class PeasyGradients
    extends Object
    Renders 1D gradients as 2D spectrums in your Processing sketch.

    The class offers both quick constructors for more simple gradients (such as 2 color horizontal gradients) and more powerful constructors for more involved gradients (centre-offset, angled, n-color gradient with color stops).

    By default, a PeasyGradients instance draws directly into the Processing sketch; you can give it a specific PGraphics pane to draw into with the .setRenderTarget() method).

    Algorithms for linear, radial & conic gradients are based on this work by Jeremy Behreandt; all others are mostly my own derivation.

    Author:
    Michael Carleton
    • Constructor Summary

      Constructors 
      Constructor Description
      PeasyGradients()
      Constructs a new PeasyGradients renderer with no rendering target.
      PeasyGradients​(processing.core.PApplet p)
      Constructs a new PeasyGradients renderer from a running Processing sketch; gradients will be rendered directly into the sketch.
      PeasyGradients​(processing.core.PImage g)
      Constructs a new PeasyGradients renderer targeting a specfic PImage.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearPosterisation()
      Clears any user-defined color posterisation setting.
      void conicGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle)
      Renders a conic gradient.
      void crossGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float zoom)
      Leading to a 'X' shape.
      void diamondGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float zoom)
      Renders a gradient where colors are plotted according to the manhattan distance between the position and midpoint, forming a diamond-shaped spectrum.
      void fractalNoiseGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float scale, micycle.peasygradients.utilities.FastNoiseLite.NoiseType noiseType, micycle.peasygradients.utilities.FastNoiseLite.FractalType fractalType, int fractalOctaves, float fractalGain, float fractalLacunarity)  
      void hourglassGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float zoom)
      Renders what I've described as a an hourglass gradient, owing to it's similarity with an hourglass at certain angles.
      void hourglassGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float zoom, float pinch, float roundness)  
      void linearGradient​(Gradient gradient, float angle)
      Renders a linear gradient (having its midpoint at the centre of the sketch/render target).
      void linearGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle)
      Renders a linear gradient with a given gradient midpoint.
      void linearGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float length)
      Renders a linear gradient using a given gradient centerpoint, angle and length.
      void linearGradient​(Gradient gradient, processing.core.PVector controlPoint1, processing.core.PVector controlPoint2)
      Renders a linear gradient using two user-defined control points, specifying the position of the first and last colors (the angle of the gradient is the angle between the two control points).
      void noiseGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float scale)
      Renders a noise gradient, using the FastNoise library to generate noise values.
      void noiseGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float scale, micycle.peasygradients.utilities.FastNoiseLite.NoiseType noiseType)
      Renders a noise gradient in the given noise type.
      void polygonGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float zoom, int sides)
      Renders a polygonal gradient
      void posterise​(int n)
      Restricts any and all rendered gradients to render in at most n colors (a.k.a.
      void radialGradient​(Gradient gradient, processing.core.PVector centerPoint, float zoom)
      Renders a radial gradient.
      void setNoiseSeed​(int seed)
      Changes the noise seed used by noise gradients.
      void setRenderTarget​(processing.core.PApplet p)
      Tells this PeasyGradients renderer to render 2D gradients into the Processing sketch (spanning the full size of the sketch).
      void setRenderTarget​(processing.core.PApplet p, int offSetX, int offSetY, int width, int height)
      Tells this PeasyGradients renderer to render 2D gradients into the Processing sketch, within a certain region specified by input arguments.
      void setRenderTarget​(processing.core.PImage g)
      Tells this PeasyGradients renderer to render 2D gradients into the PImage or PGraphics object provided by the user.
      void setRenderTarget​(processing.core.PImage g, int offSetX, int offSetY, int width, int height)
      Sets the graphics object and the rectangular region into which this PeasyGradients object will renderer gradients.
      void spiralGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float curveCount)
      Renders a spiral gradient
      void spiralGradient​(Gradient gradient, processing.core.PVector centerPoint, float angle, float curveCount, float curviness)
      Renders a spiral gradient with a specific "curviness".
      void spotlightGradient​(Gradient gradient, processing.core.PVector originPoint, float angle, float beamAngle)
      Renders a spotlight-like gradient using a given origin point, angle and light angle.
      void spotlightGradient​(Gradient gradient, processing.core.PVector originPoint, processing.core.PVector endPoint)
      where beam angle scales depending on distance between points
      void uniformNoiseGradient​(Gradient gradient, processing.core.PVector centerPoint, float z, float angle, float scale)
      Renders a noise gradient having a uniform distribution, and using a given noise z value.
    • Constructor Detail

      • PeasyGradients

        public PeasyGradients​(processing.core.PApplet p)
        Constructs a new PeasyGradients renderer from a running Processing sketch; gradients will be rendered directly into the sketch.
        Parameters:
        p - the Processing sketch. You'll usually refer to it as this
      • PeasyGradients

        public PeasyGradients​(processing.core.PImage g)
        Constructs a new PeasyGradients renderer targeting a specfic PImage.
        Parameters:
        g -
      • PeasyGradients

        public PeasyGradients()
        Constructs a new PeasyGradients renderer with no rendering target. Note setRenderTarget(PImage) must be called at least once later (before a gradient is drawn).
    • Method Detail

      • setRenderTarget

        public void setRenderTarget​(processing.core.PApplet p)
        Tells this PeasyGradients renderer to render 2D gradients into the Processing sketch (spanning the full size of the sketch).
        Parameters:
        p -
      • setRenderTarget

        public void setRenderTarget​(processing.core.PApplet p,
                                    int offSetX,
                                    int offSetY,
                                    int width,
                                    int height)
        Tells this PeasyGradients renderer to render 2D gradients into the Processing sketch, within a certain region specified by input arguments.
        Parameters:
        p -
        offSetX - x-axis offset of the region to render gradients into (where 0 is top-left corner)
        offSetY - y-axis offset of the region to render gradients into (where 0 is top-left corner)
        width - width of region to render gradients into
        height - height of region to render gradients into
      • setRenderTarget

        public void setRenderTarget​(processing.core.PImage g)
        Tells this PeasyGradients renderer to render 2D gradients into the PImage or PGraphics object provided by the user.
        Parameters:
        g - PImage or PGraphics object to render gradients into
        See Also:
        #renderIntoSketch()
      • setRenderTarget

        public void setRenderTarget​(processing.core.PImage g,
                                    int offSetX,
                                    int offSetY,
                                    int width,
                                    int height)
        Sets the graphics object and the rectangular region into which this PeasyGradients object will renderer gradients.
        Parameters:
        g -
        offSetX - x-axis offset of the region to render gradients into (where 0 is top-left corner)
        offSetY - y-axis offset of the region to render gradients into (where 0 is top-left corner)
        width - width of region to render gradients into
        height - height of region to render gradients into
      • setNoiseSeed

        public void setNoiseSeed​(int seed)
        Changes the noise seed used by noise gradients.
        Parameters:
        seed - any integer; PeasyGradients default is 0.
      • posterise

        public void posterise​(int n)
        Restricts any and all rendered gradients to render in at most n colors (a.k.a. posterisation).
        Parameters:
        n - max number of colors
        See Also:
        clearPosterisation()
      • clearPosterisation

        public void clearPosterisation()
        Clears any user-defined color posterisation setting.
        See Also:
        posterise(int)
      • linearGradient

        public void linearGradient​(Gradient gradient,
                                   float angle)
        Renders a linear gradient (having its midpoint at the centre of the sketch/render target).

        It's called 'linear' because the colors flow from left-to-right, top-to-bottom, or at any angle you chose in a single direction.

        Parameters:
        gradient - 1D gradient to use as the basis for the linear gradient
        angle - gradient angle in radians (color is drawn parallel to this angle), where a value of 0 corresponds to a horizontal gradient spanning left-to-right. larger (positive) values correspond to a clockwise direction
      • linearGradient

        public void linearGradient​(Gradient gradient,
                                   processing.core.PVector centerPoint,
                                   float angle)
        Renders a linear gradient with a given gradient midpoint. The start and end points (first and last colours) will be automatically constrained to the edges of the sketch boundary.

        It's called 'linear' because the colors flow from left-to-right, top-to-bottom, or at any angle you chose in a single direction.

        Parameters:
        gradient - 1D gradient to use as the basis for the linear gradient
        centerPoint -
        angle - in radians
      • linearGradient

        public void linearGradient​(Gradient gradient,
                                   processing.core.PVector centerPoint,
                                   float angle,
                                   float length)
        Renders a linear gradient using a given gradient centerpoint, angle and length.

        It's called 'linear' because the colors flow from left-to-right, top-to-bottom, or at any angle you chose in a single direction.

        Parameters:
        gradient - 1D gradient to use as the basis for the linear gradient
        centerPoint - gradient midpoint
        angle - in radians
        length - coefficient to lerp from centrepoint to edges (that intersect with angle). default = 1: (first and last colors will be exactly on edge); <1: colors will be sqashed; >1 colors spread out (outermost colors will leave the view).
      • linearGradient

        public void linearGradient​(Gradient gradient,
                                   processing.core.PVector controlPoint1,
                                   processing.core.PVector controlPoint2)
        Renders a linear gradient using two user-defined control points, specifying the position of the first and last colors (the angle of the gradient is the angle between the two control points).
        Parameters:
        gradient - 1D gradient to use as the basis for the linear gradient
        controlPoint1 - the location for the start point, using X (horizontal) and Y (vertical) values (can extend past the coordinates of the sketch)
        controlPoint2 - location of the end point of the gradient (can extend past the coordinates of the sketch)
      • radialGradient

        public void radialGradient​(Gradient gradient,
                                   processing.core.PVector centerPoint,
                                   float zoom)
        Renders a radial gradient.

        A radial gradient starts at a single point (the source point for color stops) and radiates outwards.

        Parameters:
        gradient - 1D gradient to use as the basis for the radial gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        zoom - default = 1
      • conicGradient

        public void conicGradient​(Gradient gradient,
                                  processing.core.PVector centerPoint,
                                  float angle)
        Renders a conic gradient.

        A conic gradient is similar to a radial gradient. Both are circular and use the centerPoint as the source point for color stops. However, where the color stops of a radial gradient emerge from the center of the circle, a conic gradient places them around the circle. Aka "angled".

        They're called 'conic' because they tend to look like the shape of a cone that is being viewed from above. Well, at least when there is a distinct angle provided and the contrast between the color values is great enough to tell a difference.

        This method creates a hard stop where the first and last colors bump right up to one another. See conicGradientSmooth() to render a conic gradient with a smooth transition between the first and last colors.

        Parameters:
        gradient - 1D gradient to use as the basis for the conic gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle - in radians, where 0 is east; going clockwise
        See Also:
        #conicGradientSmooth(Gradient, PVector, float, float)
      • spiralGradient

        public void spiralGradient​(Gradient gradient,
                                   processing.core.PVector centerPoint,
                                   float angle,
                                   float curveCount)
        Renders a spiral gradient

        A spiral gradient builds upon the conic gradient: instead of colors extending at the same angle from the midpoint (as in a conic gradient), the angle is offset by an amount proportional to the distance, creating a spiral pattern.

        Consider calling .primeAnimation() on the input gradient before rendering it as a spiral gradient to avoid creating a seam in the gradient where the first and last colors bump right up to one another.

        Parameters:
        gradient - 1D gradient to use as the basis for the spiral gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle - in radians, where 0 is east; going clockwise
        curveCount - akin to zoom
      • spiralGradient

        public void spiralGradient​(Gradient gradient,
                                   processing.core.PVector centerPoint,
                                   float angle,
                                   float curveCount,
                                   float curviness)
        Renders a spiral gradient with a specific "curviness".
        Parameters:
        gradient - 1D gradient to use as the basis for the spiral gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle - in radians, where 0 is east; going clockwise
        curveCount -
        curviness - curviness exponent. affect how distance from center point affects curve. default = 1
      • polygonGradient

        public void polygonGradient​(Gradient gradient,
                                    processing.core.PVector centerPoint,
                                    float angle,
                                    float zoom,
                                    int sides)
        Renders a polygonal gradient
        Parameters:
        gradient - 1D gradient to use as the basis for the polygon gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle - angle offset to render polygon at
        zoom -
        sides - Number of polgyon sides. Should be at least 3 (a triangle)
      • crossGradient

        public void crossGradient​(Gradient gradient,
                                  processing.core.PVector centerPoint,
                                  float angle,
                                  float zoom)
        Leading to a 'X' shape.
        Parameters:
        gradient - 1D gradient to use as the basis for the cross gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle - in radians, where 0 orients the 'X' ; going clockwise
        zoom -
      • diamondGradient

        public void diamondGradient​(Gradient gradient,
                                    processing.core.PVector centerPoint,
                                    float angle,
                                    float zoom)
        Renders a gradient where colors are plotted according to the manhattan distance between the position and midpoint, forming a diamond-shaped spectrum.
        Parameters:
        gradient - 1D gradient to use as the basis for the diamond gradient
        centerPoint - The PVector center point of the gradient — the position where it radiates from.
        angle -
        zoom -
      • noiseGradient

        public void noiseGradient​(Gradient gradient,
                                  processing.core.PVector centerPoint,
                                  float angle,
                                  float scale,
                                  micycle.peasygradients.utilities.FastNoiseLite.NoiseType noiseType)
        Renders a noise gradient in the given noise type.
        Parameters:
        gradient - 1D gradient to use as the basis for the noise gradient
        centerPoint - For noise gradients, the centre point is used only when rotating (the point to rotate around).
        angle -
        scale -
        noiseType - The type of noise: Cellular (Voronoi), Simplex, Perlin, Value, etc.
      • uniformNoiseGradient

        public void uniformNoiseGradient​(Gradient gradient,
                                         processing.core.PVector centerPoint,
                                         float z,
                                         float angle,
                                         float scale)
        Renders a noise gradient having a uniform distribution, and using a given noise z value.
        Parameters:
        gradient - 1D gradient to use as the basis for the noise gradient
        centerPoint - For noise gradients, the centre point is used only when rotating (the point to rotate around).
        z - the z value of the noise (mutate this to animate the noise)
        angle -
        scale - frequency/scale of noise. larger values are more "zoomed in"
      • fractalNoiseGradient

        public void fractalNoiseGradient​(Gradient gradient,
                                         processing.core.PVector centerPoint,
                                         float angle,
                                         float scale,
                                         micycle.peasygradients.utilities.FastNoiseLite.NoiseType noiseType,
                                         micycle.peasygradients.utilities.FastNoiseLite.FractalType fractalType,
                                         int fractalOctaves,
                                         float fractalGain,
                                         float fractalLacunarity)
        Parameters:
        gradient - 1D gradient to use as the basis for the fractal noise gradient
        centerPoint -
        angle -
        scale -
        noiseType - The type of noise: Cellular, Simplex, Perlin, Value, etc.
        fractalType - The type of fractal: FBm, Ridged, PingPong, DomainWarpProgressive, DomainWarpIndependent.
        fractalOctaves - More octaves lead to finer and finer detail. Should be at least 1. 4 is a suitable value.
        fractalGain - refers to a constant value being added to another one for each loop of an octave.
        fractalLacunarity - refers to the 'texture' of a fractal; in simple terms think of it like two noises with different sizes being overlayed making for finer and finer detail. 0...5 is a suitable range of possible values
      • spotlightGradient

        public void spotlightGradient​(Gradient gradient,
                                      processing.core.PVector originPoint,
                                      float angle,
                                      float beamAngle)
        Renders a spotlight-like gradient using a given origin point, angle and light angle.
        Parameters:
        gradient - 1D gradient to use as the basis for the spotlight gradient
        originPoint - the PVector point where the spotlight originates from
        angle - the angle of the spotlightangle = 0 means spotlight (facing down)
        beamAngle - Determines the angle (in radians) of the light beam. Smaller values mean a narrower beam, larger values mean a wider beam. Range is 0...PI (a.k.a. 0...180 in degrees). A default value would be PI/2 (90degrees).
      • spotlightGradient

        public void spotlightGradient​(Gradient gradient,
                                      processing.core.PVector originPoint,
                                      processing.core.PVector endPoint)
        where beam angle scales depending on distance between points
        Parameters:
        gradient - 1D gradient to use as the basis for the spotlight gradient
        originPoint -
        endPoint -
      • hourglassGradient

        public void hourglassGradient​(Gradient gradient,
                                      processing.core.PVector centerPoint,
                                      float angle,
                                      float zoom)
        Renders what I've described as a an hourglass gradient, owing to it's similarity with an hourglass at certain angles.
        Parameters:
        gradient - 1D gradient to use as the basis for the hourglass gradient
        centerPoint -
        angle -
        zoom - default = 1
        See Also:
        hourglassGradient(Gradient, PVector, float, float, float, float)
      • hourglassGradient

        public void hourglassGradient​(Gradient gradient,
                                      processing.core.PVector centerPoint,
                                      float angle,
                                      float zoom,
                                      float pinch,
                                      float roundness)
        Parameters:
        gradient -
        centerPoint -
        angle -
        zoom -
        pinch - determines how "pinched" the join/neck between the two bulbs is. default is 0
        roundness - how rounded (or circular) the bulbs are. default = 1
        See Also:
        hourglassGradient(Gradient, PVector, float, float)