-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathColor.dart
100 lines (99 loc) · 2.91 KB
/
Color.dart
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
part of BigIsland;
// Big Island video game source code file
// Copyright (C) 2012 Severin Ibarluzea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Big Island video game source code file
// Copyright (C) 2012 Severin Ibarluzea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
class Color {
int _r,_g,_b;
void set r(int x){
_r = (x > 255) ? 255 : (x < 0) ? 0 : x;
}
int get r => _r;
void set g(int x){
_g = (x > 255) ? 255 : (x < 0) ? 0 : x;
}
int get g => _g;
void set b(int x){
_b = (x > 255) ? 255 : (x < 0) ? 0 : x;
}
int get b => _b;
Color(r,g,b){
this.r = r;
this.g = g;
this.b = b;
}
Color.fromString(String s){
List ar = s.splitChars();
if (s.length == 4){
r = int.parse("0x${ar[1]}");
r = (r<<4) + r;
g = int.parse("0x${ar[2]}");
g = (g<<4) + g;
b = int.parse("0x${ar[3]}");
b = (b<<4) + b;
}else if (s.length == 7){
r = int.parse("0x${ar[1]}${ar[2]}");
g = int.parse("0x${ar[3]}${ar[4]}");
b = int.parse("0x${ar[5]}${ar[6]}");
}
}
String toString(){
String s = ((r<<16) | (g<<8) | (b)).toRadixString(16);
while(s.length<6){
s = "0$s";
}
return "#$s";
}
Color multiply(num x){
r = (r * x).toInt();
g = (g * x).toInt();
b = (b * x).toInt();
return this;
}
Color divide(num x){
r = (r / x).toInt();
g = (g / x).toInt();
b = (b / x).toInt();
return this;
}
Color subtract(int x){
r -= x;
g -= x;
b -= x;
return this;
}
void blend(Color color,[num w = .5]){
r = (r * (1 - w) + color.r * w).toInt();
g = (g * (1 - w) + color.g * w).toInt();
b = (b * (1 - w) + color.b * w).toInt();
}
Color clone(){
return new Color(r,g,b);
}
}