Class Color

java.lang.Object
org.openpatch.scratch.Color

public final class Color extends Object
A colour. Colours never change: withHue(…) and the other with methods return a new colour, so a colour can be shared, kept in a constant (HtmlColor) or handed to several sprites without one of them changing it for the others.

A colour can be given in two ways:

  • red, green and blue from 0 to 255, plus an optional alpha (opacity) from 0 to 255: new Color(255, 128, 0);
  • Scratch's colour, saturation and brightness, each from 0 to 100: Color.fromHSB(50, 100, 100). The colour (hue) goes around: 0 is red, about 17 yellow, 33 green, 50 cyan, 67 blue, 83 magenta, 100 red again.
Color orange = new Color(255, 128, 0);
Color lighter = orange.withBrightness(100).withSaturation(50);
Color next = orange.withHue(orange.getHue() + 10);
  • Constructor Summary

    Constructors
    Constructor
    Description
    Color(double red, double green, double blue)
    A colour from red, green and blue, each from 0 to 255 (values outside are clamped).
    Color(double red, double green, double blue, double alpha)
    A colour from red, green, blue and alpha, each from 0 to 255 (values outside are clamped).
    A colour from a hex code as in HTML and CSS: "#ff8000", "ff8000", "#f80", or with alpha "#ff800080".
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(Object other)
    Two colours are equal when they pack into the same ARGB number.
    static Color
    fromARGB(int argb)
    A colour from a packed ARGB number, as toARGB() gives it.
    static Color
    fromHSB(double hue, double saturation, double brightness)
    A colour from Scratch's colour, saturation and brightness, each from 0 to 100.
    double
     
    double
     
    double
     
    double
     
    double
     
    double
     
    double
     
    int
     
    int
    The colour packed into one int as alpha, red, green and blue (8 bits each, rounded), the way pixels are stored.
     
    withAlpha(double alpha)
     
    withBlue(double blue)
     
    withBrightness(double brightness)
     
    withGreen(double green)
     
    withHue(double hue)
    Another colour (hue), keeping saturation, brightness and alpha.
    withRed(double red)
     
    withSaturation(double saturation)
     

    Methods inherited from class Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Color

      public Color(double red, double green, double blue)
      A colour from red, green and blue, each from 0 to 255 (values outside are clamped). The colour is opaque.
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
    • Color

      public Color(double red, double green, double blue, double alpha)
      A colour from red, green, blue and alpha, each from 0 to 255 (values outside are clamped). An alpha of 0 is invisible, 255 opaque.
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
      alpha - opacity from 0 to 255
    • Color

      public Color(String hex)
      A colour from a hex code as in HTML and CSS: "#ff8000", "ff8000", "#f80", or with alpha "#ff800080".
      Parameters:
      hex - the hex code
  • Method Details

    • fromHSB

      public static Color fromHSB(double hue, double saturation, double brightness)
      A colour from Scratch's colour, saturation and brightness, each from 0 to 100. The colour goes around (110 is the same as 10); saturation and brightness are clamped.
      Parameters:
      hue - the colour from 0 to 100
      saturation - from 0 (grey) to 100 (full colour)
      brightness - from 0 (black) to 100 (full brightness)
      Returns:
      the colour
    • fromARGB

      public static Color fromARGB(int argb)
      A colour from a packed ARGB number, as toARGB() gives it.
      Parameters:
      argb - alpha, red, green and blue in one int
      Returns:
      the colour
    • getRed

      public double getRed()
      Returns:
      red from 0 to 255
    • getGreen

      public double getGreen()
      Returns:
      green from 0 to 255
    • getBlue

      public double getBlue()
      Returns:
      blue from 0 to 255
    • getAlpha

      public double getAlpha()
      Returns:
      alpha (opacity) from 0 (invisible) to 255 (opaque)
    • getHue

      public double getHue()
      Returns:
      Scratch's colour (hue) from 0 to 100; 0 for greys
    • getSaturation

      public double getSaturation()
      Returns:
      saturation from 0 (grey) to 100
    • getBrightness

      public double getBrightness()
      Returns:
      brightness from 0 (black) to 100
    • withRed

      public Color withRed(double red)
      Parameters:
      red - red from 0 to 255
      Returns:
      this colour with another red
    • withGreen

      public Color withGreen(double green)
      Parameters:
      green - green from 0 to 255
      Returns:
      this colour with another green
    • withBlue

      public Color withBlue(double blue)
      Parameters:
      blue - blue from 0 to 255
      Returns:
      this colour with another blue
    • withAlpha

      public Color withAlpha(double alpha)
      Parameters:
      alpha - opacity from 0 to 255
      Returns:
      this colour with another alpha
    • withHue

      public Color withHue(double hue)
      Another colour (hue), keeping saturation, brightness and alpha. The colour goes around, so withHue(getHue() + 10) works like Scratch's "change color by 10".
      Parameters:
      hue - the colour from 0 to 100
      Returns:
      this colour with another hue
    • withSaturation

      public Color withSaturation(double saturation)
      Parameters:
      saturation - from 0 to 100
      Returns:
      this colour with another saturation
    • withBrightness

      public Color withBrightness(double brightness)
      Parameters:
      brightness - from 0 to 100
      Returns:
      this colour with another brightness
    • toARGB

      public int toARGB()
      The colour packed into one int as alpha, red, green and blue (8 bits each, rounded), the way pixels are stored.
      Returns:
      the packed colour
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      #rrggbb, or #rrggbbaa when not opaque
    • equals

      public boolean equals(Object other)
      Two colours are equal when they pack into the same ARGB number.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object