Class ColorPalette


  • public class ColorPalette
    extends java.lang.Object
    Color palette for terminal color management and conversion.

    The ColorPalette class provides functionality for managing terminal colors, including color conversion between different formats (RGB, ANSI, indexed), color distance calculation, and color remapping. It helps bridge the gap between the different color capabilities of various terminals.

    This class supports various terminal color modes:

    • 8-color mode (standard ANSI colors)
    • 16-color mode (standard ANSI colors + bright variants)
    • 256-color mode (indexed colors)
    • 24-bit true color mode (RGB colors)

    The palette can be used to convert between these color modes, find the closest matching color in a more limited palette, and even modify the terminal's color palette on supported terminals.

    This class is used internally by JLine components to handle color output across different terminal types with varying color support capabilities.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canChange()
      Check if the terminal has the capability to change colors.
      int getColor​(int index)
      Get a specific color in the palette
      int getDefaultBackground()
      Get the terminal's default background color.
      int getDefaultForeground()
      Get the terminal's default foreground color.
      protected org.jline.utils.Colors.Distance getDist()  
      java.lang.String getDistanceName()
      Get the name of the distance to use for rounding colors.
      int getLength()
      Get the palette length
      boolean isReal()  
      boolean loadPalette()
      Load the palette from the terminal.
      protected void loadPalette​(boolean doLoad)  
      int round​(int col)  
      int round​(int r, int g, int b)  
      void setColor​(int index, int color)
      Change the color of the palette
      void setDistance​(java.lang.String name)
      Set the name of the color distance to use when rounding RGB colors to the palette.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ColorPalette

        public ColorPalette()
      • ColorPalette

        public ColorPalette​(Terminal terminal)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • ColorPalette

        public ColorPalette​(Terminal terminal,
                            java.lang.String distance)
                     throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • getDistanceName

        public java.lang.String getDistanceName()
        Get the name of the distance to use for rounding colors.
        Returns:
        the name of the color distance
      • setDistance

        public void setDistance​(java.lang.String name)
        Set the name of the color distance to use when rounding RGB colors to the palette.
        Parameters:
        name - the name of the color distance
      • canChange

        public boolean canChange()
        Check if the terminal has the capability to change colors.
        Returns:
        true if the terminal can change colors
      • loadPalette

        public boolean loadPalette()
                            throws java.io.IOException
        Load the palette from the terminal. If the palette has already been loaded, subsequent calls will simply return true.
        Returns:
        true if the palette has been successfully loaded.
        Throws:
        java.io.IOException
      • loadPalette

        protected void loadPalette​(boolean doLoad)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • getLength

        public int getLength()
        Get the palette length
        Returns:
        the palette length
      • getColor

        public int getColor​(int index)
        Get a specific color in the palette
        Parameters:
        index - the index of the color
        Returns:
        the color at the given index
      • setColor

        public void setColor​(int index,
                             int color)
        Change the color of the palette
        Parameters:
        index - the index of the color
        color - the new color value
      • isReal

        public boolean isReal()
      • round

        public int round​(int r,
                         int g,
                         int b)
      • round

        public int round​(int col)
      • getDist

        protected org.jline.utils.Colors.Distance getDist()
      • getDefaultForeground

        public int getDefaultForeground()
        Get the terminal's default foreground color.
        Returns:
        the RGB value of the default foreground color, or -1 if not available
      • getDefaultBackground

        public int getDefaultBackground()
        Get the terminal's default background color.
        Returns:
        the RGB value of the default background color, or -1 if not available
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object