-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorize.go
135 lines (107 loc) · 3.18 KB
/
colorize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package colorstrings
import (
"fmt"
"strings"
"github.com/pandasoli/colorstrings/Errors"
. "github.com/pandasoli/colorstrings/Color"
. "github.com/pandasoli/colorstrings/colors"
. "github.com/pandasoli/colorstrings/Field"
. "github.com/pandasoli/colorstrings/colors/TwoBitColor"
. "github.com/pandasoli/colorstrings/colors/FourBitColor"
. "github.com/pandasoli/colorstrings/colors/EightBitColor"
. "github.com/pandasoli/colorstrings/colors/RGBColor"
)
func (self *ColorString) Colorize(str_fields string, position uint) error {
if len(self.String) < int(position) {
msg := fmt.Sprintf("Cannot colorize after the string's end (%d < %d)", len(self.String), position)
return fmt.Errorf(msg)
}
var fields []Field
// --- Making fields ---
for _, str_field := range strings.Split(str_fields, ";") {
field, err := MakeField(str_field)
if err != nil { return err }
fields = append(fields, field)
}
// --- Making color ---
expr := Color {
Position: position,
}
add := func(color IColor) {
switch color.GetKind() {
case FOREGROUND: expr.Foreground = color
case BACKGROUND: expr.Background = color
}
}
for i := 0; i < len(fields); i++ {
field := fields[i]
if 107 < field.Value {
msg := fmt.Sprintf("A field non-RGB cannot be bigger than 107 (%d)", field.Value)
return fmt.Errorf(msg)
}
if err := TryTwoBitColor(field); err.Kind != errors.NO_ERROR {
if err.Kind == errors.WRONG_PROPS {
return err.Err
}
// errors.WRONG_COLOR
} else {
add(NewTwoBitColor(field))
continue
}
if err := TryFourBitColor(field); err.Kind != errors.NO_ERROR {
if err.Kind == errors.WRONG_PROPS {
return err.Err
}
// errors.WRONG_COLOR
} else {
add(NewFourBitColor(field))
continue
}
if err := TryEightBitColor(fields[i:]); err.Kind != errors.NO_ERROR {
if err.Kind == errors.WRONG_PROPS { return err.Err }
if err.Kind == errors.NO_PREFIX { return err.Err }
if err.Kind == errors.WRONG_FIELDS { return err.Err }
// errors.WRONG_COLOR
} else {
add(NewEightBitColor(fields[i:]))
i += 2
continue
}
if err := TryRGBColor(fields[i:]); err.Kind != errors.NO_ERROR {
if err.Kind == errors.WRONG_PROPS { return err.Err }
if err.Kind == errors.NO_PREFIX { return err.Err }
if err.Kind == errors.WRONG_FIELDS { return err.Err }
// errors.WRONG_COLOR
} else {
add(NewRGBColor(fields[i:]))
i += 4
continue
}
expr.FontEffects = append(expr.FontEffects, field)
}
// --- Finding a nice position ---
// replace the color at the same position's attributes
// or append a new one if there's none at the same position
var new_i int
var found_new_i bool
for i, old_color := range self.Colors {
if old_color.Position == expr.Position {
found_new_i = true
self.Colors[i].Sum(expr)
}
if old_color.Position >= expr.Position {
break
}
new_i++
}
if !found_new_i {
self.Colors = append(
self.Colors[:new_i],
append(
[]Color { expr },
self.Colors[new_i:]...,
)...,
)
}
return nil
}