-
Notifications
You must be signed in to change notification settings - Fork 0
Colors
Javier Darkona edited this page Aug 31, 2025
·
1 revision
The io.github.darkona.logged.colors package provides color primitives and named palettes used by Logged’s decorators and plugins. It abstracts ANSI escape sequences and offers both simple ANSI colors and 24‑bit RGB (truecolor).
- Package:
src/main/java/io/github/darkona/logged/colors/-
ColorEnum— common contract for all colors -
LogColor— 24‑bit RGB palette (truecolor) -
BasicColor— basic ANSI color palette (8/16‑color codes) - Named palettes:
Blue,Red,Green,Yellow,Orange,Pink,White,Gray,Brown -
ColorFinder— resolve a color from a string (name, hex, or ANSI)
-
- Minimal interface implemented by all color enums.
- Methods
-
toString()→ ANSI escape sequence for the foreground color. -
red()/green()/blue()→ component accessors (0–255). ForBasicColorthey return 0 (non‑RGB). - Static:
reset()→ ANSI reset sequence"\u001B[0m".
-
- Helper:
assemble(r,g,b)builds a truecolor foreground sequence"\u001B[38;2;<r>;<g>;<b>m".
Usage
String c = LogColor.GOLD.toString(); // start gold
String reset = ColorEnum.reset(); // reset attributes- Enum of common colors with 24‑bit RGB values (e.g.,
RED(255,0,0),GOLD(255,215,0)). -
toString()returns a 24‑bit foreground ANSI code38;2;r;g;b. - Prefer
LogColorwhen your terminal/sink supports truecolor.
Example
System.out.println(LogColor.CYAN + "Cyan text" + ColorEnum.reset());- Enum mapping classic ANSI SGR codes (e.g.,
RED(31),BLUE(34),WHITE(97)). -
toString()returns"\u001B[0;<code>m". - Use when targeting limited terminals or when truecolor is not desired.
Example
System.out.println(BasicColor.GREEN + "OK" + ColorEnum.reset());- Enums with curated shades for families like
Blue,Red,Green, etc. - Each constant carries RGB (truecolor) and
toString()usesColorEnum#assemble. - Useful for consistent thematic output (e.g., WARN vs OK vs ERROR palettes).
Examples
System.out.println(Blue.DODGER_BLUE + "hello" + ColorEnum.reset());
System.out.println(Red.CRIMSON + "fail" + ColorEnum.reset());Available families
-
Blue,Red,Green,Yellow,Orange,Pink,White,Gray,Brown.
-
ColorFinder.findColor(String)resolves aColorEnumfrom flexible inputs:- Hex:
#RRGGBBorRRGGBB(e.g.,#ff3674). - Name (case/space/dash insensitive): matches constants in
BasicColor, then families (Blue,Red, …). - ANSI string (back‑compat): matches an exact
toString()of known colors.
- Hex:
- Order of resolution (first match wins):
BasicColor→ family enums → ANSI string → defaultBasicColor.BLACK.
Examples
ColorEnum c1 = ColorFinder.findColor("#ff3674"); // hex
ColorEnum c2 = ColorFinder.findColor("dodger-blue"); // Blue.DODGER_BLUE
ColorEnum c3 = ColorFinder.findColor("RED"); // BasicColor.RED-
LogDecoratorandColorizerconsumeColorEnum:- Decorator color methods often accept
ColorEnumor strings resolved viaColorFinder. - Combine with
ColorEnum.reset()to ensure styles don’t leak.
- Decorator color methods often accept
Example
String banner = colorDecorator.custom(LogColor.ORANGE.red(), LogColor.ORANGE.green(), LogColor.ORANGE.blue(), "Hi");- Terminals/sinks:
- Truecolor (RGB) requires support for
38;2SGR; most modern terminals support it. - Basic ANSI is more widely supported; when in doubt, prefer
BasicColor.
- Truecolor (RGB) requires support for
- Reset after styling with
ColorEnum.reset()to avoid bleeding styles. - For log files processed by systems that don’t render ANSI, strip codes (see
Colorizer.clearColor). - Colors affect foreground only; background color helpers are exposed by
LogDecorator(e.g.,customBg).