Package squidpony

Class IColorCenter.Skeleton<T>

java.lang.Object
squidpony.IColorCenter.Skeleton<T>
Type Parameters:
T - a concrete color type
All Implemented Interfaces:
IColorCenter<T>
Enclosing interface:
IColorCenter<T>

public abstract static class IColorCenter.Skeleton<T>
extends Object
implements IColorCenter<T>
A skeletal implementation of IColorCenter.
Author:
smelC
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    protected class  IColorCenter.Skeleton.GranularHasher  

    Nested classes/interfaces inherited from interface squidpony.IColorCenter

    IColorCenter.Skeleton<T>
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected IFilter<T> filter  
    protected IColorCenter.Skeleton.GranularHasher theHasher  
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected Skeleton​(IFilter<T> filter)  
  • Method Summary

    Modifier and Type Method Description
    int cacheSize()
    The actual cache is not public, but there are cases where you may want to know how many different colors are actually used in a frame or a section of the game.
    void clearCache()
    It clears the cache.
    void copyCache​(IColorCenter.Skeleton<T> other)
    You may want to copy colors between IColorCenter instances that have different create() methods -- and as such, will have different values for the same keys in the cache.
    protected abstract T create​(int red, int green, int blue, int opacity)
    Create a concrete instance of the color type given as a type parameter.
    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)  
    T getBlack()  
    T getHSV​(float hue, float saturation, float value)  
    T getHSV​(float hue, float saturation, float value, float opacity)  
    float getHue​(float r, float g, float b)  
    float getHue​(T c)  
    T getRandom​(IRNG rng, int opacity)  
    float getSaturation​(float r, float g, float b)  
    float getSaturation​(T c)  
    T getTransparent()  
    protected long getUniqueIdentifier​(int r, int g, int b, int a)  
    protected long getUniqueIdentifier​(T item)  
    float getValue​(float r, float g, float b)  
    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.
    IColorCenter.Skeleton<T> setFilter​(IFilter<T> filter)
    If you're changing the filter, you should likely call clearCache().

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface squidpony.IColorCenter

    getAlpha, getBlue, getGreen, getRed
  • Field Details

  • Constructor Details

  • Method Details

    • clearCache

      public void clearCache()
      It clears the cache. You may need to do this to limit the cache to the colors used in a specific section. This is also useful if a Filter changes what colors it should return on a frame-by-frame basis; in that case, you can call clearCache() at the start or end of a frame to ensure the next frame gets different colors.
    • cacheSize

      public int cacheSize()
      The actual cache is not public, but there are cases where you may want to know how many different colors are actually used in a frame or a section of the game. If the cache was emptied (which might be from calling clearCache()), some colors were requested, then this is called, the returned int should be the count of distinct colors this IColorCenter had created and cached; duplicates won't be counted twice.
      Returns:
    • copyCache

      public void copyCache​(IColorCenter.Skeleton<T> other)
      You may want to copy colors between IColorCenter instances that have different create() methods -- and as such, will have different values for the same keys in the cache. This allows you to copy the cache from other into this Skeleton, but using this Skeleton's create() method.
      Parameters:
      other - another Skeleton of the same type that will have its cache copied into this Skeleton
    • setFilter

      public IColorCenter.Skeleton<T> setFilter​(IFilter<T> filter)
      If you're changing the filter, you should likely call clearCache().
      Parameters:
      filter - The filter to use, or null to turn filtering OFF.
      Returns:
      this
    • get

      public T get​(int red, int green, int blue, int opacity)
      Specified by:
      get in interface IColorCenter<T>
      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

      public T get​(int red, int green, int blue)
      Specified by:
      get in interface IColorCenter<T>
      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

      public T getHSV​(float hue, float saturation, float value, float opacity)
      Specified by:
      getHSV in interface IColorCenter<T>
      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

      public T getHSV​(float hue, float saturation, float value)
      Specified by:
      getHSV in interface IColorCenter<T>
      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

      public T getWhite()
      Specified by:
      getWhite in interface IColorCenter<T>
      Returns:
      Opaque white.
    • getBlack

      public T getBlack()
      Specified by:
      getBlack in interface IColorCenter<T>
      Returns:
      Opaque black.
    • getTransparent

      public T getTransparent()
      Specified by:
      getTransparent in interface IColorCenter<T>
      Returns:
      The fully transparent color.
    • getRandom

      public T getRandom​(IRNG rng, int opacity)
      Specified by:
      getRandom in interface IColorCenter<T>
      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.
    • getSaturation

      public float getSaturation​(float r, float g, float b)
      Parameters:
      r - the red component in 0.0 to 1.0 range, typically
      g - the green component in 0.0 to 1.0 range, typically
      b - the blue component in 0.0 to 1.0 range, typically
      Returns:
      the saturation of the color from 0.0 (a grayscale color; inclusive) to 1.0 (a bright color, exclusive)
    • getSaturation

      public float getSaturation​(T c)
      Specified by:
      getSaturation in interface IColorCenter<T>
      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

      public float getValue​(float r, float g, float b)
      Parameters:
      r - the red component in 0.0 to 1.0 range, typically
      g - the green component in 0.0 to 1.0 range, typically
      b - the blue component in 0.0 to 1.0 range, typically
      Returns:
      the value (essentially lightness) of the color from 0.0 (black, inclusive) to 1.0 (very bright, inclusive).
    • getValue

      public float getValue​(T c)
      Specified by:
      getValue in interface IColorCenter<T>
      Parameters:
      c - a concrete color
      Returns:
      the value (essentially lightness) of the color from 0.0 (black, inclusive) to 1.0 (very bright, inclusive).
    • getHue

      public float getHue​(float r, float g, float b)
      Parameters:
      r - the red component in 0.0 to 1.0 range, typically
      g - the green component in 0.0 to 1.0 range, typically
      b - the blue component in 0.0 to 1.0 range, typically
      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)
    • getHue

      public float getHue​(T c)
      Specified by:
      getHue in interface IColorCenter<T>
      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)
    • filter

      public T filter​(T c)
      Specified by:
      filter in interface IColorCenter<T>
      Returns:
      The color that this shows when c is requested. May be c itself.
    • filter

      public IColoredString<T> filter​(IColoredString<T> ics)
      Specified by:
      filter in interface IColorCenter<T>
      Returns:
      ics filtered according to IColorCenter.filter(Object). May be ics itself if unchanged.
    • greify

      public 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."
      Specified by:
      greify in interface IColorCenter<T>
      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

      public 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. This implementation tries to work with colors in a way that is as general as possible, using getRed() instead of some specific detail that depends on how a color is implemented. Other implementations that specialize in a specific type of color may be able to be more efficient.
      Specified by:
      lerp in interface IColorCenter<T>
      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

      public 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.
      Specified by:
      desaturated in interface IColorCenter<T>
      Parameters:
      color - the color T to desaturate (will not be modified)
      Returns:
      the grayscale version of color
    • desaturate

      public 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.
      Specified by:
      desaturate in interface IColorCenter<T>
      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

      public 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.
      Specified by:
      saturated in interface IColorCenter<T>
      Parameters:
      color - the color T to saturate (will not be modified)
      Returns:
      the saturated version of color
    • saturate

      public 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 is likely to produce a red hue by default (if there's no hue to make vivid, it needs to choose something).
      Specified by:
      saturate in interface IColorCenter<T>
      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

      public ArrayList<T> gradient​(T fromColor, T toColor)
      Description copied from interface: IColorCenter
      Finds a gradient with 16 steps going from fromColor to toColor, both included in the gradient.
      Specified by:
      gradient in interface IColorCenter<T>
      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

      public ArrayList<T> gradient​(T fromColor, T toColor, int steps)
      Description copied from interface: IColorCenter
      Finds a gradient with the specified number of steps going from fromColor to toColor, both included in the gradient.
      Specified by:
      gradient in interface IColorCenter<T>
      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
    • create

      protected abstract T create​(int red, int green, int blue, int opacity)
      Create a concrete instance of the color type given as a type parameter. That's the place to use the filter.
      Parameters:
      red - the red component of the desired color
      green - the green component of the desired color
      blue - the blue component of the desired color
      opacity - the alpha component or opacity of the desired color
      Returns:
      a fresh instance of the concrete color type
    • getUniqueIdentifier

      protected long getUniqueIdentifier​(int r, int g, int b, int a)
    • getUniqueIdentifier

      protected long getUniqueIdentifier​(T item)