-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.odin
64 lines (56 loc) · 1.26 KB
/
profile.odin
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
package karvi
import "core:strings"
import "core:strconv"
import "colorful"
Profile :: enum {
Undefined,
// TrueColor, 24-bit color profile
True_Color,
// ANSI256, 8-bit color profile
ANSI256,
// ANSI, 4-bit color profile
ANSI,
// Ascii, uncolored profile
Ascii,
}
// Convert transforms a given Color to a Color supported within the Profile.
profile_convert :: proc(p: Profile, c: ^Color) -> ^Color {
using Profile
if p == Ascii do return new_no_color()
switch v in c.type {
case ANSI_Color:
return v
case ANSI256_Color:
if p == ANSI do return ansi256_to_ansi(v)
return v
case RGB_Color:
h, err := colorful.hex(v.color)
if err != 0 do return nil
if p != True_Color {
ac := hex_to_ansi256(h)
if p == ANSI do return ansi256_to_ansi(ac.type.(ANSI256_Color))
return ac
}
return v
case No_Color:
return new_no_color()
}
return c
}
// Color creates a Color from a string. Valid inputs are hex colors, as well as
// ANSI color codes (0-15, 16-255).
color :: proc(p: Profile, s: string) -> ^Color {
if len(s) == 0 do return nil
c: ^Color
if strings.has_prefix(s, "#") {
c = new_rgb_color(s)
} else {
i := strconv.atoi(s)
if i < 16 {
c = new_ansi_color(i)
} else {
c = new_ansi256_color(i)
}
}
return profile_convert(p, c)
}