Package squidpony

Interface IColorCenter<T>

Type Parameters:
T - The concrete type of colors
All Known Implementing Classes:
IColorCenter.Skeleton

public interface IColorCenter<T>
How to manage colors, making sure that a color is allocated at most once.

If you aren't using squidlib's gdx part, you should use this interface (and the IColorCenter.Skeleton implementation), because it caches instances.

If you are using squidlib's gdx part, you should use this interface (and the SquidColorCenter implementation) if:

  • You don't want to use preallocated instances (if you do, check out squidpony.squidgrid.gui.Colors)
  • You don't want to use named colors (if you do, check out com.badlogic.gdx.graphics.Colors)
  • You don't like libgdx's Color representation (components as floats in-between 0 and 1) but prefer components within 0 (inclusive) and 256 (exclusive); and don't mind the overhead of switching the representations. My personal opinion is that the overhead doesn't matter w.r.t other intensive operations that we have in roguelikes (path finding).
Author:
smelC
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Interface Description
    static class  IColorCenter.Skeleton<T>
    A skeletal implementation of IColorCenter.
  • Method Summary

    Modifier and Type Method Description
    T desaturate​(T color, float degree)
    Brings a color closer to grayscale by the specified degree and returns the new color (desaturated somewhat).
    T desaturated​(T color)
    Gets a fully-desaturated version of the given color (keeping its brightness, but making it grayscale).
    IColoredString<T> filter​(IColoredString<T> ics)  
    T filter​(T c)  
    T get​(int red, int green, int blue)  
    T get​(int red, int green, int blue, int opacity)  
    int getAlpha​(T c)  
    T getBlack()  
    int getBlue​(T c)  
    int getGreen​(T c)  
    T getHSV​(float hue, float saturation, float value)  
    T getHSV​(float hue, float saturation, float value, float opacity)  
    float getHue​(T c)  
    T getRandom​(IRNG rng, int opacity)  
    int getRed​(T c)  
    float getSaturation​(T c)  
    T getTransparent()  
    float getValue​(T c)  
    T getWhite()  
    ArrayList<T> gradient​(T fromColor, T toColor)
    Finds a gradient with 16 steps going from fromColor to toColor, both included in the gradient.
    ArrayList<T> gradient​(T fromColor, T toColor, int steps)
    Finds a gradient with the specified number of steps going from fromColor to toColor, both included in the gradient.
    T greify​(T t, boolean doAlpha)
    Gets a copy of t and modifies it to make a shade of gray with the same brightness.
    T lerp​(T start, T end, float change)
    Gets the linear interpolation from Color start to Color end, changing by the fraction given by change.
    T saturate​(T color, float degree)
    Saturates color (makes it closer to a vivid color like red or green and less gray) by the specified degree and returns the new color (saturated somewhat).
    T saturated​(T color)
    Fully saturates color (makes it a vivid color like red or green and less gray) and returns the modified copy.
  • Method Details

    • get

      T get​(int red, int green, int blue, int opacity)
      Parameters:
      red - The red component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      green - The green component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      blue - The blue component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      opacity - The alpha component. In-between 0 (inclusive) and 256 (exclusive). Larger values mean more opacity; 0 is clear.
      Returns:
      A possibly transparent color.
    • get

      T get​(int red, int green, int blue)
      Parameters:
      red - The red component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      green - The green component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      blue - The blue component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
      Returns:
      An opaque color.
    • getHSV

      T getHSV​(float hue, float saturation, float value, float opacity)
      Parameters:
      hue - The hue of the desired color from 0.0 (red, inclusive) towards orange, then yellow, and eventually to purple before looping back to almost the same red (1.0, exclusive). Values outside this range should be treated as wrapping around, so 1.1f and -0.9f would be the same as 0.1f .
      saturation - the saturation of the color from 0.0 (a grayscale color; inclusive) to 1.0 (a bright color, inclusive)
      value - the value (essentially lightness) of the color from 0.0 (black, inclusive) to 1.0 (very bright, inclusive).
      opacity - the alpha component as a float; 0.0f is clear, 1.0f is opaque.
      Returns:
      a possibly transparent color
    • getHSV

      T getHSV​(float hue, float saturation, float value)
      Parameters:
      hue - The hue of the desired color from 0.0 (red, inclusive) towards orange, then yellow, and eventually to purple before looping back to almost the same red (1.0, exclusive)
      saturation - the saturation of the color from 0.0 (a grayscale color; inclusive) to 1.0 (a bright color, exclusive)
      value - the value (essentially lightness) of the color from 0.0 (black, inclusive) to 1.0 (very bright, inclusive).
      Returns:
      an opaque color
    • getWhite

      Returns:
      Opaque white.
    • getBlack

      Returns:
      Opaque black.
    • getTransparent

      Returns:
      The fully transparent color.
    • getRandom

      T getRandom​(IRNG rng, int opacity)
      Parameters:
      rng - an RNG from SquidLib.
      opacity - The alpha component. In-between 0 (inclusive) and 256 (exclusive). Larger values mean more opacity; 0 is clear.
      Returns:
      A random color, except for the alpha component.
    • getRed

      int getRed​(T c)
      Parameters:
      c - a concrete color
      Returns:
      The red component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
    • getGreen

      int getGreen​(T c)
      Parameters:
      c - a concrete color
      Returns:
      The green component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
    • getBlue

      int getBlue​(T c)
      Parameters:
      c - a concrete color
      Returns:
      The blue component. For screen colors, in-between 0 (inclusive) and 256 (exclusive).
    • getAlpha

      int getAlpha​(T c)
      Parameters:
      c - a concrete color
      Returns:
      The alpha component. In-between 0 (inclusive) and 256 (exclusive).
    • getHue

      float getHue​(T c)
      Parameters:
      c - a concrete color
      Returns:
      The hue of the color from 0.0 (red, inclusive) towards orange, then yellow, and eventually to purple before looping back to almost the same red (1.0, exclusive)
    • getSaturation

      float getSaturation​(T c)
      Parameters:
      c - a concrete color
      Returns:
      the saturation of the color from 0.0 (a grayscale color; inclusive) to 1.0 (a bright color, exclusive)
    • getValue

      float getValue​(T c)
      Parameters:
      c - a concrete color
      Returns:
      the value (essentially lightness) of the color from 0.0 (black, inclusive) to 1.0 (very bright, inclusive).
    • filter

      T filter​(T c)
      Parameters:
      c -
      Returns:
      The color that this shows when c is requested. May be c itself.
    • filter

      Parameters:
      ics -
      Returns:
      ics filtered according to filter(Object). May be ics itself if unchanged.
    • greify

      T greify​(T t, boolean doAlpha)
      Gets a copy of t and modifies it to make a shade of gray with the same brightness. The doAlpha parameter causes the alpha to be considered in the calculation of brightness and also changes the returned alpha of the color. Not related to reified types or any usage of "reify."
      Parameters:
      t - a T to copy; only the copy will be modified
      doAlpha - Whether to include (and hereby change) the alpha component.
      Returns:
      A monochromatic variation of t.
    • lerp

      T lerp​(T start, T end, float change)
      Gets the linear interpolation from Color start to Color end, changing by the fraction given by change.
      Parameters:
      start - the initial color T
      end - the "target" color T
      change - the degree to change closer to end; a change of 0.0f produces start, 1.0f produces end
      Returns:
      a new T between start and end
    • desaturated

      T desaturated​(T color)
      Gets a fully-desaturated version of the given color (keeping its brightness, but making it grayscale). Keeps alpha the same; if you want alpha to be considered (and brightness to be calculated differently), then you can use greify() in this class instead.
      Parameters:
      color - the color T to desaturate (will not be modified)
      Returns:
      the grayscale version of color
    • desaturate

      T desaturate​(T color, float degree)
      Brings a color closer to grayscale by the specified degree and returns the new color (desaturated somewhat). Alpha is left unchanged.
      Parameters:
      color - the color T to desaturate
      degree - a float between 0.0f and 1.0f; more makes it less colorful
      Returns:
      the desaturated (and if a filter is used, also filtered) new color T
    • saturated

      T saturated​(T color)
      Fully saturates color (makes it a vivid color like red or green and less gray) and returns the modified copy. Leaves alpha unchanged.
      Parameters:
      color - the color T to saturate (will not be modified)
      Returns:
      the saturated version of color
    • saturate

      T saturate​(T color, float degree)
      Saturates color (makes it closer to a vivid color like red or green and less gray) by the specified degree and returns the new color (saturated somewhat). If this is called on a color that is very close to gray, this does not necessarily return a specific color, but most implementations will treat a hue of 0 as red.
      Parameters:
      color - the color T to saturate
      degree - a float between 0.0f and 1.0f; more makes it more colorful
      Returns:
      the saturated (and if a filter is used, also filtered) new color
    • gradient

      ArrayList<T> gradient​(T fromColor, T toColor)
      Finds a gradient with 16 steps going from fromColor to toColor, both included in the gradient.
      Parameters:
      fromColor - the color to start with, included in the gradient
      toColor - the color to end on, included in the gradient
      Returns:
      an ArrayList composed of the blending steps from fromColor to toColor, with length equal to steps
    • gradient

      ArrayList<T> gradient​(T fromColor, T toColor, int steps)
      Finds a gradient with the specified number of steps going from fromColor to toColor, both included in the gradient.
      Parameters:
      fromColor - the color to start with, included in the gradient
      toColor - the color to end on, included in the gradient
      steps - the number of elements to use in the gradient
      Returns:
      an ArrayList composed of the blending steps from fromColor to toColor, with length equal to steps