|
| 1 | +# Colorize |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`Colorize` is a library of coloring and decorating a string by Escape Sequence. |
| 6 | +There are 2 ways of decoration. |
| 7 | + |
| 8 | +1. Use a `String` methods directly. |
| 9 | +2. Use a `Colorize` class. |
| 10 | + |
| 11 | +Here it is described about `Colorize` class. |
| 12 | +See [String](../primitive/string.md) for how to use `String` methods directly. |
| 13 | + |
| 14 | +## Colorize |
| 15 | + |
| 16 | +### Method Chain |
| 17 | + |
| 18 | +`Colorize` have following methods to change an attribute of a string. |
| 19 | + |
| 20 | +| Method | Meaning | |
| 21 | +| ------------- | ---------------------------------------------- | |
| 22 | +| `black()` | Sets black as a foreground color. | |
| 23 | +| `red()` | Sets red as a foreground color. | |
| 24 | +| `green()` | Sets green as a foreground color. | |
| 25 | +| `yellow()` | Sets yellow as a foreground color. | |
| 26 | +| `blue()` | Sets blue as a foreground color. | |
| 27 | +| `magenta()` | Sets magenta as a foreground color. | |
| 28 | +| `cyan()` | Sets cyan as a foreground color. | |
| 29 | +| `white()` | Sets white as a foreground color. | |
| 30 | +| `onBlack()` | Sets black as a background color. | |
| 31 | +| `onRed()` | Sets red as a background color. | |
| 32 | +| `onGreen()` | Sets green as a background color. | |
| 33 | +| `onYellow()` | Sets yellow as a background color. | |
| 34 | +| `onBlue()` | Sets blue as a background color. | |
| 35 | +| `onMagenta()` | Sets magenta as a background color. | |
| 36 | +| `onCyan()` | Sets cyan as a background color. | |
| 37 | +| `onWhite()` | Sets white as a background color. | |
| 38 | +| `bold()` | Sets a bold style. | |
| 39 | +| `underline()` | Sets an uderline. | |
| 40 | +| `reverse()` | Exchanges a foreground and a background color. | |
| 41 | +| `clear()` | Clears all attributes. | |
| 42 | +| `end()` | Ends a right previous attribute change. | |
| 43 | +| `text(str)` | Sets a text with a changed attribute. | |
| 44 | + |
| 45 | +### Parsing String |
| 46 | + |
| 47 | +The `parse` method of a `Colorize` instance will recognize some tags for coloring and decorating. |
| 48 | + |
| 49 | +| Method | Meaning | |
| 50 | +| ---------------------------- | ---------------------------------------------- | |
| 51 | +| `<black>...</black>` | Sets black as a foreground color. | |
| 52 | +| `<red>...</red>` | Sets red as a foreground color. | |
| 53 | +| `<green>...</green>` | Sets green as a foreground color. | |
| 54 | +| `<yellow>...</yellow>` | Sets yellow as a foreground color. | |
| 55 | +| `<blue>...</blue>` | Sets blue as a foreground color. | |
| 56 | +| `<magenta>...</magenta>` | Sets magenta as a foreground color. | |
| 57 | +| `<cyan>...</cyan>` | Sets cyan as a foreground color. | |
| 58 | +| `<white>...</white>` | Sets white as a foreground color. | |
| 59 | +| `<onBlack>...</onBlack>` | Sets black as a background color. | |
| 60 | +| `<onRed>...</onRed>` | Sets red as a background color. | |
| 61 | +| `<onGreen>...</onGreen>` | Sets green as a background color. | |
| 62 | +| `<onYellow>...</onYellow>` | Sets yellow as a background color. | |
| 63 | +| `<onBlue>...</onBlue>` | Sets blue as a background color. | |
| 64 | +| `<onMagenta>...</onMagenta>` | Sets magenta as a background color. | |
| 65 | +| `<onCyan>...</onCyan>` | Sets cyan as a background color. | |
| 66 | +| `<onWhite>...</onWhite>` | Sets white as a background color. | |
| 67 | +| `<bold>..</bold>` | Sets a bold style. | |
| 68 | +| `<underline>..</underline>` | Sets an uderline. | |
| 69 | +| `<reverse>..</reverse>` | Exchanges a foreground and a background color. | |
| 70 | + |
| 71 | +You can use tags above as follows. |
| 72 | + |
| 73 | +```javascript |
| 74 | +var colorize = new Colorize(); |
| 75 | +System.println(colorize.parse("<yellow><bold>Yellow</bold> Text</yellow>")); |
| 76 | +``` |
| 77 | + |
| 78 | +## Output Example |
| 79 | + |
| 80 | +Here is an example. |
| 81 | + |
| 82 | +```javascript |
| 83 | +var colorize = new Colorize(); |
| 84 | +System.println( |
| 85 | + "Head:" + |
| 86 | + colorize |
| 87 | + .red() |
| 88 | + .text("Red") |
| 89 | + .bold() |
| 90 | + .text("Red/Bold") |
| 91 | + .yellow() |
| 92 | + .text("Yellow/Bold") |
| 93 | + .onRed() |
| 94 | + .underline() |
| 95 | + .text("Yellow/Bold/Underlined") |
| 96 | + .end() // Underline ended |
| 97 | + .text("<-->") |
| 98 | + .end() // BgColor |
| 99 | + .end() // Yellow ended |
| 100 | + .text("Red/Bold") |
| 101 | + .end() // Bold ended |
| 102 | + .text("Red") |
| 103 | + + ":Tail" |
| 104 | +); |
| 105 | + |
| 106 | +System.println( |
| 107 | + colorize.parse( |
| 108 | + "<red>" |
| 109 | + "Yes, " |
| 110 | + "<yellow>" |
| 111 | + "<underline>\\<bv\\></underline>" |
| 112 | + " at <bold>the <onRed>bottom," |
| 113 | + " or <reverse><blink>top</blink></reverse></onRed></bold>" |
| 114 | + " of <underline>row</underline>" |
| 115 | + "</yellow>" |
| 116 | + "</red>" |
| 117 | + ) |
| 118 | +); |
| 119 | +``` |
| 120 | + |
| 121 | +Here is an actual result. |
| 122 | + |
| 123 | + |
| 124 | + |
0 commit comments