Scoop API

scoop:scoop.terminal:0.1.0

src/style/color.scoop

Selected production source · src/style/color.scoop

1package scoop.terminal.style
3/**
4 * A terminal palette, indexed, RGB, or default-reset color. RGB components and palette indices are
5 * validated when serialized.
6 */
7public enum Color {
8 /**
9 * The terminal's default color for the selected channel.
10 */
11 Reset,
12 /**
13 * The Black entry in the terminal's configurable base palette; its exact RGB appearance
14 * belongs to the terminal theme.
15 */
16 Black,
17 /**
18 * The DarkGrey entry in the terminal's configurable base palette; its exact RGB appearance
19 * belongs to the terminal theme.
20 */
21 DarkGrey,
22 /**
23 * The Red entry in the terminal's configurable base palette; its exact RGB appearance
24 * belongs to the terminal theme.
25 */
26 Red,
27 /**
28 * The DarkRed entry in the terminal's configurable base palette; its exact RGB appearance
29 * belongs to the terminal theme.
30 */
31 DarkRed,
32 /**
33 * The Green entry in the terminal's configurable base palette; its exact RGB appearance
34 * belongs to the terminal theme.
35 */
36 Green,
37 /**
38 * The DarkGreen entry in the terminal's configurable base palette; its exact RGB
39 * appearance belongs to the terminal theme.
40 */
41 DarkGreen,
42 /**
43 * The Yellow entry in the terminal's configurable base palette; its exact RGB appearance
44 * belongs to the terminal theme.
45 */
46 Yellow,
47 /**
48 * The DarkYellow entry in the terminal's configurable base palette; its exact RGB
49 * appearance belongs to the terminal theme.
50 */
51 DarkYellow,
52 /**
53 * The Blue entry in the terminal's configurable base palette; its exact RGB appearance belongs
54 * to the terminal theme.
55 */
56 Blue,
57 /**
58 * The DarkBlue entry in the terminal's configurable base palette; its exact RGB appearance
59 * belongs to the terminal theme.
60 */
61 DarkBlue,
62 /**
63 * The Magenta entry in the terminal's configurable base palette; its exact RGB appearance
64 * belongs to the terminal theme.
65 */
66 Magenta,
67 /**
68 * The DarkMagenta entry in the terminal's configurable base palette; its exact RGB
69 * appearance belongs to the terminal theme.
70 */
71 DarkMagenta,
72 /**
73 * The Cyan entry in the terminal's configurable base palette; its exact RGB appearance
74 * belongs to the terminal theme.
75 */
76 Cyan,
77 /**
78 * The DarkCyan entry in the terminal's configurable base palette; its exact RGB appearance
79 * belongs to the terminal theme.
80 */
81 DarkCyan,
82 /**
83 * The White entry in the terminal's configurable base palette; its exact RGB appearance
84 * belongs to the terminal theme.
85 */
86 White,
87 /**
88 * The Grey entry in the terminal's configurable base palette; its exact RGB appearance
89 * belongs to the terminal theme.
90 */
91 Grey,
92 /**
93 * An indexed ANSI palette color in 0..255.
94 */
95 AnsiValue(val value: Int),
96 /**
97 * A true-color triple whose components must each be in 0..255.
98 */
99 Rgb(val red: Int, val green: Int, val blue: Int)
101 /**
102 * Named-color and ANSI color-parameter parsing.
103 */
104 public companion object {
105 /**
106 * Parses a supported named color, returning None for unrecognized input.
107 */
108 public fun parseOrNone(text: String): Color? = parseColorName(text)
109 /**
110 * Parses a supported named color or throws for unrecognized input.
111 */
112 public fun parse(text: String): Color = when (parseOrNone(text)) {
113 case Some(val color) -> color
114 case None -> throw IllegalArgumentException(Some("unknown terminal color"))
115 }
116 /**
117 * Parses a supported ANSI color parameter representation, returning None for invalid
118 * input.
119 */
120 public fun parseAnsiOrNone(text: String): Color? = parseAnsiColor(text)
121 }
124/**
125 * Associates a color with foreground, background, or underline channel.
126 */
127public enum Colored {
128 /**
129 * A foreground-channel color.
130 */
131 ForegroundColor(val color: Color),
132 /**
133 * A background-channel color.
134 */
135 BackgroundColor(val color: Color),
136 /**
137 * An underline-channel color.
138 */
139 UnderlineColor(val color: Color)
141 /**
142 * Returns the SGR color parameter text for the selected channel, without the surrounding CSI
143 * command.
144 */
145 public fun toAnsiString(): String = when (this) {
146 case ForegroundColor(val color) -> colorParameters(color, 38)
147 case BackgroundColor(val color) -> colorParameters(color, 48)
148 case UnderlineColor(val color) -> colorParameters(color, 58)
149 }
151 /**
152 * Parses ANSI color parameters while retaining their channel.
153 */
154 public companion object {
155 /**
156 * Parses color parameters and returns their channel and value, or None on invalid input.
157 */
158 public fun parseAnsiOrNone(text: String): Colored? = parseColored(text)
159 }
162/**
163 * Optional foreground and background selections; absent channels are left unchanged.
164 */
165public struct Colors(val foreground: Color? = None, val background: Color? = None)
167internal fun requireColorByte(value: Int) {
168 if (value < 0 || value > 255) throw IllegalArgumentException(Some("color component outside 0..255"))
171internal fun colorParameters(color: Color, channel: Int): String {
172 val value = when (color) {
173 case Color.Reset -> return (channel + 1).toString()
174 case Color.Black -> "5;0"; case Color.DarkRed -> "5;1"
175 case Color.DarkGreen -> "5;2"; case Color.DarkYellow -> "5;3"
176 case Color.DarkBlue -> "5;4"; case Color.DarkMagenta -> "5;5"
177 case Color.DarkCyan -> "5;6"; case Color.Grey -> "5;7"
178 case Color.DarkGrey -> "5;8"; case Color.Red -> "5;9"
179 case Color.Green -> "5;10"; case Color.Yellow -> "5;11"
180 case Color.Blue -> "5;12"; case Color.Magenta -> "5;13"
181 case Color.Cyan -> "5;14"; case Color.White -> "5;15"
182 case Color.AnsiValue(val index) -> {
183 requireColorByte(index)
184 "5;" + index.toString()
185 }
186 case Color.Rgb(val red, val green, val blue) -> {
187 requireColorByte(red); requireColorByte(green); requireColorByte(blue)
188 "2;" + red.toString() + ";" + green.toString() + ";" + blue.toString()
189 }
190 }
191 return channel.toString() + ";" + value