forked from kjlaw89/viup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.v
70 lines (56 loc) · 1.43 KB
/
color.v
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
module viup
pub struct Color {
pub mut:
r u8
g u8
b u8
a u8 = 255
}
// parse_color parses the provided color string
// and converts it to a valid `Color`
pub fn parse_color(color string) Color {
mut obj := Color{}
if color == '' {
return obj
}
split := color.split(' ')
if split.len > 0 {
obj.r = split[0].u8()
}
if split.len > 1 {
obj.g = split[1].u8()
}
if split.len > 2 {
obj.b = split[2].u8()
}
if split.len > 3 {
obj.a = split[3].u8()
}
return obj
}
// show_picker is a helper function to show a color_dialog. It
// will automatically show the color dialog with this `Color`s
// values pre-selected and returns back the new Color on select
// as well as all of the colors in the color table
pub fn (color Color) show_picker() (Color, []Color) {
dialog := color_dialog('value=${color.r} ${color.g} ${color.b}', 'alpha=${color.a}',
'showhex=yes', 'showcolortable=yes', 'title=Color Chooser')
dialog.popup(pos_current, pos_current)
if dialog.get_bool('status') {
value := dialog.get_attr('value')
colortable := dialog.get_attr('colortable')
mut colors := []Color{}
split := colortable.split(';')
for c in split {
colors << parse_color(c)
}
parsed := parse_color(value)
return parsed, colors
}
return color, []Color{len: 0}
}
// str converts the color to a IUP valid string
// Example format: 200 150 0 100
pub fn (color Color) str() string {
return '${color.r} ${color.g} ${color.b} ${color.a}'
}