Class GColor

java.lang.Object
com.iizix.prop.GColor
All Implemented Interfaces:
Cloneable

public final class GColor extends Object implements Cloneable
This is a data holder class for color, typically an RGB(A) color for a component.

The class is FINAL and its instances are immutable value objects. A color is in one of three STATES: a literal ARGB value, the CSS inherit keyword, or a REFERENCE to a palette color by name (ofRef(String), persisted as %<reference>). The state is carried by discriminator fields (isInherited, ref), never by a subclass and never by a reserved value smuggled into value - a sentinel inside the payload can always collide with a real payload, a separate tag cannot. The precedent is INHERIT, which is identified by its flag and not by its value, precisely because its value -1 is also opaque white.

A referenced color carries NO resolved value: what it means depends on the palette and on the active scheme at run time, so equals(GColor) and hashCode() compare the reference and never a resolved value, and getColor() reports the unresolved magenta until something resolves it (ColorResolver). On the wire (appendColor(SendTransaction,GColor)) the reference travels as its name plus a fallback color computed at send time. The role names a component asks for are ColorRoles.

Being final is what keeps equals(GColor) honest. Java binds overloads on the STATIC type, so with a subclass in play a.equals(b) and ((Object)a).equals(b) could return different answers for the same two values depending only on how the variables were declared - invisible, and unexplainable from any log. No subclass, one dispatch, no asymmetry.

Author:
Christopher Mindus
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final GColor
    The inherited color, used for CSS.
    static final HashMap<Integer,String>
    The map of colors to name where only the first entry is taken.
    static final GColor
    The no-color, i.e.
    static final char
    The character that starts the persisted form of a referenced color: %<reference>.
    static final GColor
    The unresolved color as a literal: what a referenced color paints as when nothing resolves it.
    static final int
    The ARGB value a referenced color reports while unresolved: opaque magenta, the missing-texture color that no designer chooses and that stays violently wrong under the common color-vision deficiencies (ruled 2026-09-12, "The fallback, corrected").
  • Constructor Summary

    Constructors
    Constructor
    Description
    GColor(int rgb)
    Creates an opaque RGB color object.
    GColor(int argb, boolean hasAlpha)
    Creates an opaque [A]RGB color object.
    GColor(int r, int g, int b)
    Creates an opaque RGB color object.
    GColor(int r, int g, int b, double a)
    Creates an ARGB color object.
    GColor(int r, int g, int b, int a)
    Creates an ARGB color object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Appends the color to a transaction.
    void
    Appends the color to a transaction, with the fallback a referenced color carries on the wire.
    static GColor
    Creates a GColor from a String.
    boolean
    equals(GColor color)
    Checks if two color objects are equal.
    boolean
    Checks if two color objects are equal.
    static GColor
    Creates a color from a transaction, the wire form appendColor(SendTransaction,GColor) writes: a tag byte, 0 literal, 1 inherit, 2 reference.
    static long
    Gets an opaque ARGB color value from a CSS3 color name.
    int
    Gets the color value as ARGB.
    static String[]
    Returns an array copy of all the color names sorted in alphabetical order.
    static List<String>
    Returns a list of all the color names sorted in alphabetical order.
    Gets the shortest CSS color string for this color.
    Returns the color name.
    Gets the palette reference of a referenced color.
    static int
    Gets a color RGB value from a CSS3 color name, WITHOUT the alpha component.
    Gets the color as a String in the form described below: 0xaarrggbb for a literal, inherit, or %<reference> for a referenced color - the persisted form, read back by createGColor(String).
    int
    Hash code.
    boolean
    Returns whether the color is inherited or not.
    boolean
    Checks if this color is (100%) opaque and not a color by name or not a system color, i.e.
    boolean
    Returns whether this color is a reference to a palette color.
    boolean
    Checks if this color is transparent, or no color is set.
    static GColor
    Creates a referenced color: a color that names a palette color instead of holding a value.
    protected String
    Returns the parameter string representing the state of this event.
    static GColor
    Parses an HTML color String, such as "#nnn", "#123456", "rgb(r,g,b)", "rgba(r,g,b,a)" or a named color.
    Returns a string representation of this class instance and its values.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • NONE

      public static final GColor NONE
      The no-color, i.e. full transparent.
    • INHERIT

      public static final GColor INHERIT
      The inherited color, used for CSS.
    • REF_PREFIX

      public static final char REF_PREFIX
      The character that starts the persisted form of a referenced color: %<reference>.
      See Also:
    • UNRESOLVED_ARGB

      public static final int UNRESOLVED_ARGB
      The ARGB value a referenced color reports while unresolved: opaque magenta, the missing-texture color that no designer chooses and that stays violently wrong under the common color-vision deficiencies (ruled 2026-09-12, "The fallback, corrected").
      See Also:
    • UNRESOLVED

      public static final GColor UNRESOLVED
      The unresolved color as a literal: what a referenced color paints as when nothing resolves it.
    • namesForColors

      public static final HashMap<Integer,String> namesForColors
      The map of colors to name where only the first entry is taken.
  • Constructor Details

    • GColor

      public GColor(int r, int g, int b)
      Creates an opaque RGB color object.
      Parameters:
      r - The red color (0-255).
      g - The green color (0-255).
      b - The build color (0-255).
      Throws:
      IllegalArgumentException - if any r,g,b color is not 0-255.
    • GColor

      public GColor(int r, int g, int b, int a)
      Creates an ARGB color object.
      Parameters:
      r - The red color (0-255).
      g - The green color (0-255).
      b - The build color (0-255).
      a - The alpha (0-255), 0=transparent, 255=opaque.
      Throws:
      IllegalArgumentException - if any r,g,b,a value is not 0-255.
    • GColor

      public GColor(int r, int g, int b, double a)
      Creates an ARGB color object.
      Parameters:
      r - The red color (0-255).
      g - The green color (0-255).
      b - The build color (0-255).
      a - The alpha (0.0-1.0), 0.0=transparent, 1.0=opaque.
      Throws:
      IllegalArgumentException - if any r,g,b color is not 0-255, or if a is NaN or not 0.0-1.0.
    • GColor

      public GColor(int rgb)
      Creates an opaque RGB color object.
      Parameters:
      rgb - The integer red, green and blue colors.
      Throws:
      IllegalArgumentException - if rgb < 0 or rgb > 0xFFFFFF.
    • GColor

      public GColor(int argb, boolean hasAlpha)
      Creates an opaque [A]RGB color object.
      Parameters:
      argb - The alpha-blended integer red, green and blue colors.
      hasAlpha - If false, sets alpha-blend part to 255.
      Throws:
      IllegalArgumentException - if hasAlpha is false and argb < 0 or argb > 0xFFFFFF.
  • Method Details

    • getColorNames

      public static String[] getColorNames()
      Returns an array copy of all the color names sorted in alphabetical order. To avoid cloning, use the method getColorNamesList() instead.
      Returns:
      A cloned array of the color names.
    • getColorNamesList

      public static List<String> getColorNamesList()
      Returns a list of all the color names sorted in alphabetical order.
      Returns:
      An immutable list of color names.
    • getRGBColorFromCSS3Name

      public static int getRGBColorFromCSS3Name(String name)
      Gets a color RGB value from a CSS3 color name, WITHOUT the alpha component.

      The returned value is 0x00RRGGBB, in the range 0 to 0xFFFFFF, so the -1 returned for an unknown name cannot be mistaken for a color. The mask is applied even though the backing map holds no alpha today: it makes the contract true by construction rather than by coincidence, so a change to the map cannot silently turn this into an ARGB method.

      Use getARGBColorFromCSS3Name(String) when an opaque ARGB value is wanted. It cannot return an int for the reason this method can: opaque ARGB values occupy the whole 32-bit range, so -1 would be 0xFFFFFFFF, which is opaque white - a real color, and a common one. That is the same collision documented on equals(GColor) for INHERIT.

      Parameters:
      name - The color name, case-insensitive.
      Returns:
      The RGB color value 0 to 0xFFFFFF matching the name, or -1 for none.
      Throws:
      NullPointerException - if name is null.
    • getARGBColorFromCSS3Name

      public static long getARGBColorFromCSS3Name(String name)
      Gets an opaque ARGB color value from a CSS3 color name.

      The returned value is 0xFFRRGGBB as a long, which is what every GColor constructor taking an RGB value produces. A long is used deliberately: as an int the natural error value -1 is 0xFFFFFFFF, i.e. opaque white, so no int-returning version of this method can report "not a color name" without also being unable to report white. As a long the color values occupy 0x00000000L to 0xFFFFFFFFL and -1L lies outside that range.

      Parameters:
      name - The color name, case-insensitive.
      Returns:
      The opaque ARGB color value 0xFF000000L to 0xFFFFFFFFL matching the name, or -1L for none.
      Throws:
      NullPointerException - if name is null.
    • createGColor

      public static GColor createGColor(String string)
      Creates a GColor from a String. If the string is null or empty, GColor.NONE is returned, otherwise a new GColor as if new GColor(string) was called.
      Parameters:
      string - The string as 0xnnnnnnnn or decimal. It can be null or empty string for GColor.NONE, "inherit" for GColor.INHERIT, or "%" followed by a palette reference for a referenced color (ofRef(String)).
      Throws:
      IllegalArgumentException - for errors in the string specification, including an empty reference after "%".
      NumberFormatException - for invalid number formats.
    • parseGColor

      public static GColor parseGColor(String string) throws ParseException
      Parses an HTML color String, such as "#nnn", "#123456", "rgb(r,g,b)", "rgba(r,g,b,a)" or a named color. Named colors are CSS3 names or IIZI palette references starting with "%", which keep their case: a reference is a property path and Brand/Primary is not brand/primary.
      Throws:
      ParseException - If parsing failed.
    • ofRef

      public static GColor ofRef(String ref)
      Creates a referenced color: a color that names a palette color instead of holding a value.

      The reference is a property reference string, e.g. MyModule:/style/Brand/Primary, a module-relative /style/Brand/Primary, or a bare role name such as Accent that is resolved against the module's palette and then the platform roles (ColorRoles); see ColorResolver. It is kept exactly as given, case included. Persisted as %<reference> (getString()) and created back by createGColor(String).

      Parameters:
      ref - The reference, never null or blank, without the leading %.
      Returns:
      A new referenced color.
      Throws:
      IllegalArgumentException - If ref is null or blank.
    • from

      public static GColor from(ReadTransaction trans)
      Creates a color from a transaction, the wire form appendColor(SendTransaction,GColor) writes: a tag byte, 0 literal, 1 inherit, 2 reference.

      For a reference the name is read and the fallback the sender computed is read and DISCARDED: the receiver holds the reference and resolves it against its own palette. A resolved value is never part of a referenced color.

      Parameters:
      trans - The transaction.
      Returns:
      The color.
      Throws:
      IllegalArgumentException - If the tag byte is not 0, 1 or 2.
    • getName

      public String getName()
      Returns the color name.
      Returns:
      The color name is either a CSS3 color name matching the value or a predefined color name, or null if the color is not mapped to a named color. A referenced color has no CSS3 name (its reference is getRef()): the unresolved magenta it reports must not come back as "fuchsia".
    • isReference

      public boolean isReference()
      Returns whether this color is a reference to a palette color.
      Returns:
      true for a referenced color (ofRef(String)), false for a literal or inherited color.
    • getRef

      public String getRef()
      Gets the palette reference of a referenced color.
      Returns:
      The reference without the leading %, or null for a literal or inherited color.
    • appendColor

      public void appendColor(SendTransaction trans)
      Appends the color to a transaction.

      The wire form is a tag byte: 0 literal, then the 32-bit ARGB value; 1 inherit, nothing follows; 2 reference, then the reference name, then the fallback color computed at send time as a literal-or-inherit (a boolean, then the ARGB value when not inherited). The tag replaced a boolean on 2026-09-26 (ruling R16): client and server are always the same build, so there is no version to negotiate, and a separate tag is what makes a third state impossible to confuse with a value.

      A referenced color appended through THIS method has no resolver, so its fallback is the UNRESOLVED magenta and a warning is logged; a holder that can resolve the reference against its tree calls appendColor(SendTransaction,GColor) instead (ColorProp, Color4Prop).

      Parameters:
      trans - The transaction.
    • appendColor

      public void appendColor(SendTransaction trans, GColor fallback)
      Appends the color to a transaction, with the fallback a referenced color carries on the wire.

      For a literal or inherited color the fallback is ignored and the form is that of appendColor(SendTransaction). For a referenced color the tag 2 is written, then the reference name, then fallback as a boolean (inherited) and, when not inherited, its ARGB value: the color the client paints until something resolves the name.

      Parameters:
      trans - The transaction.
      fallback - The resolved color to carry as the fallback, a literal or INHERIT; UNRESOLVED when the reference did not resolve.
      Throws:
      IllegalArgumentException - If fallback is itself a referenced color.
      NullPointerException - If fallback is null and this color is a reference.
    • equals

      public boolean equals(Object o)
      Checks if two color objects are equal.
      Overrides:
      equals in class Object
    • equals

      public boolean equals(GColor color)
      Checks if two color objects are equal.

      The inherited color is equal only to itself: the flag is the discriminator, not the value. INHERIT carries the value -1, which is also the value of opaque white (0xFFFFFFFF), so a comparison that looked only at the value would call the two equal.

      A referenced color is equal only to a color with the same reference, and never to a literal, whatever it currently resolves to: resolution changes with the active scheme at run time and equality must not, or a color used as a map key becomes unfindable after a scheme switch (ruled 2026-09-12, Q12: compare the reference, never what it currently means).

      Parameters:
      color - another color object. If null, the colors are not equal.
      Returns:
      true if equal, false otherwise.
    • hashCode

      public int hashCode()
      Hash code.

      The discriminator is mixed in for the same reason equals(GColor) tests it: two colors that are never equal must not hash equally, or they collide in every bucket. The class itself is no longer part of this - the class is final, so it is a constant. A referenced color hashes on its reference alone, as it compares.

      Overrides:
      hashCode in class Object
      Returns:
      Returns the hash code.
    • isInherited

      public boolean isInherited()
      Returns whether the color is inherited or not.
      Returns:
      true for CSS "inherit" color, false otherwise.
    • isTransparent

      public boolean isTransparent()
      Checks if this color is transparent, or no color is set.
      Returns:
      true if transparent, false otherwise (also false when inherited, and false for a referenced color, which reports its unresolved opaque magenta).
    • isOpaque

      public boolean isOpaque()
      Checks if this color is (100%) opaque and not a color by name or not a system color, i.e. is a color of RGB type or ARGB with A=0x00.
      Returns:
      true if fully opaque, false otherwise (also false when inherited).
    • getColor

      public int getColor()
      Gets the color value as ARGB.
      Returns:
      The color value, -1 if inherited, UNRESOLVED_ARGB for a referenced color.
    • getString

      public String getString()
      Gets the color as a String in the form described below: 0xaarrggbb for a literal, inherit, or %<reference> for a referenced color - the persisted form, read back by createGColor(String).
      Returns:
      the String as described below.
    • paramString

      protected String paramString()
      Returns the parameter string representing the state of this event. This string is useful for debugging. Subclasses adds extra information to the string by preceding it with a comma followed by the extra information.
      Returns:
      the parameter string of this event.
    • toString

      public String toString()
      Returns a string representation of this class instance and its values.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this class instance.
    • getCSSValue

      public String getCSSValue()
      Gets the shortest CSS color string for this color.

      A referenced color returns the CSS of its unresolved magenta, #F0F: this class resolves nothing. The var(--izc-...) a referenced color becomes on the client is the delivery phase's (color.js), and a build-time consumer wants a resolved literal from ColorResolver.

      Returns:
      E.g. #123456 for opaque colors, rgba(r,g,b,a) for (semi-)transparency colors, or "inherit" if inherited.