rays 0.1.12 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.doc/ext/rays/bitmap.cpp +22 -76
- data/.doc/ext/rays/bounds.cpp +95 -125
- data/.doc/ext/rays/camera.cpp +171 -0
- data/.doc/ext/rays/color.cpp +223 -45
- data/.doc/ext/rays/color_space.cpp +146 -46
- data/.doc/ext/rays/defs.cpp +183 -0
- data/.doc/ext/rays/font.cpp +69 -21
- data/.doc/ext/rays/image.cpp +26 -37
- data/.doc/ext/rays/matrix.cpp +186 -29
- data/.doc/ext/rays/native.cpp +14 -8
- data/.doc/ext/rays/noise.cpp +53 -0
- data/.doc/ext/rays/painter.cpp +187 -292
- data/.doc/ext/rays/point.cpp +96 -77
- data/.doc/ext/rays/polygon.cpp +313 -0
- data/.doc/ext/rays/polygon_line.cpp +96 -0
- data/.doc/ext/rays/polyline.cpp +167 -0
- data/.doc/ext/rays/rays.cpp +103 -12
- data/.doc/ext/rays/shader.cpp +83 -9
- data/LICENSE +21 -0
- data/README.md +1 -1
- data/Rakefile +24 -9
- data/VERSION +1 -1
- data/ext/rays/bitmap.cpp +23 -81
- data/ext/rays/bounds.cpp +100 -128
- data/ext/rays/camera.cpp +186 -0
- data/ext/rays/color.cpp +231 -51
- data/ext/rays/color_space.cpp +149 -47
- data/ext/rays/defs.cpp +183 -0
- data/ext/rays/defs.h +26 -2
- data/ext/rays/extconf.rb +2 -3
- data/ext/rays/font.cpp +74 -24
- data/ext/rays/image.cpp +28 -40
- data/ext/rays/matrix.cpp +198 -30
- data/ext/rays/native.cpp +14 -8
- data/ext/rays/noise.cpp +55 -0
- data/ext/rays/painter.cpp +203 -298
- data/ext/rays/point.cpp +105 -81
- data/ext/rays/polygon.cpp +329 -0
- data/ext/rays/polygon_line.cpp +99 -0
- data/ext/rays/polyline.cpp +176 -0
- data/ext/rays/rays.cpp +103 -13
- data/ext/rays/shader.cpp +84 -9
- data/include/rays.h +10 -2
- data/include/rays/bitmap.h +14 -26
- data/include/rays/bounds.h +21 -4
- data/include/rays/camera.h +74 -0
- data/include/rays/color.h +25 -14
- data/include/rays/color_space.h +15 -10
- data/include/rays/coord.h +114 -0
- data/include/rays/debug.h +22 -0
- data/include/rays/defs.h +36 -0
- data/include/rays/exception.h +6 -2
- data/include/rays/font.h +4 -4
- data/include/rays/image.h +12 -18
- data/include/rays/matrix.h +50 -24
- data/include/rays/noise.h +42 -0
- data/include/rays/opengl.h +2 -50
- data/include/rays/painter.h +89 -93
- data/include/rays/point.h +44 -51
- data/include/rays/polygon.h +198 -0
- data/include/rays/polyline.h +71 -0
- data/include/rays/rays.h +3 -0
- data/include/rays/ruby.h +7 -1
- data/include/rays/ruby/bounds.h +1 -1
- data/include/rays/ruby/camera.h +41 -0
- data/include/rays/ruby/color.h +1 -1
- data/include/rays/ruby/color_space.h +1 -1
- data/include/rays/ruby/font.h +1 -1
- data/include/rays/ruby/matrix.h +1 -1
- data/include/rays/ruby/point.h +1 -1
- data/include/rays/ruby/polygon.h +52 -0
- data/include/rays/ruby/polyline.h +41 -0
- data/include/rays/ruby/rays.h +8 -0
- data/include/rays/ruby/shader.h +1 -1
- data/include/rays/shader.h +36 -8
- data/lib/rays.rb +7 -2
- data/lib/rays/bitmap.rb +0 -15
- data/lib/rays/bounds.rb +17 -23
- data/lib/rays/camera.rb +24 -0
- data/lib/rays/color.rb +20 -47
- data/lib/rays/color_space.rb +13 -13
- data/lib/rays/image.rb +3 -7
- data/lib/rays/matrix.rb +28 -0
- data/lib/rays/module.rb +4 -19
- data/lib/rays/painter.rb +78 -93
- data/lib/rays/point.rb +13 -21
- data/lib/rays/polygon.rb +58 -0
- data/lib/rays/polygon_line.rb +36 -0
- data/lib/rays/polyline.rb +32 -0
- data/lib/rays/shader.rb +20 -1
- data/rays.gemspec +5 -7
- data/src/bitmap.h +36 -0
- data/src/bounds.cpp +74 -11
- data/src/color.cpp +58 -23
- data/src/color_space.cpp +52 -34
- data/src/color_space.h +22 -0
- data/src/coord.cpp +170 -0
- data/src/coord.h +35 -0
- data/src/font.cpp +118 -0
- data/src/font.h +64 -0
- data/src/frame_buffer.cpp +37 -71
- data/src/frame_buffer.h +4 -4
- data/src/image.cpp +172 -98
- data/src/image.h +25 -0
- data/src/ios/bitmap.h +23 -0
- data/src/ios/bitmap.mm +133 -110
- data/src/ios/camera.mm +510 -0
- data/src/ios/font.mm +50 -62
- data/src/ios/helper.h +4 -4
- data/src/ios/opengl.mm +19 -4
- data/src/ios/rays.mm +3 -0
- data/src/matrix.cpp +111 -26
- data/src/matrix.h +30 -0
- data/src/noise.cpp +74 -0
- data/src/opengl.cpp +9 -27
- data/src/opengl.h +37 -0
- data/src/osx/bitmap.h +23 -0
- data/src/osx/bitmap.mm +133 -110
- data/src/osx/camera.mm +451 -0
- data/src/osx/font.mm +49 -62
- data/src/osx/helper.h +2 -2
- data/src/osx/opengl.mm +19 -83
- data/src/osx/rays.mm +3 -0
- data/src/painter.cpp +845 -671
- data/src/painter.h +24 -0
- data/src/point.cpp +140 -119
- data/src/polygon.cpp +1266 -0
- data/src/polygon.h +32 -0
- data/src/polyline.cpp +160 -0
- data/src/polyline.h +69 -0
- data/src/render_buffer.cpp +11 -4
- data/src/render_buffer.h +2 -2
- data/src/shader.cpp +163 -106
- data/src/shader.h +38 -0
- data/src/shader_program.cpp +533 -0
- data/src/{program.h → shader_program.h} +28 -16
- data/src/shader_source.cpp +140 -0
- data/src/shader_source.h +52 -0
- data/src/texture.cpp +136 -160
- data/src/texture.h +65 -0
- data/src/win32/bitmap.cpp +62 -52
- data/src/win32/font.cpp +11 -13
- data/src/win32/font.h +24 -0
- data/src/win32/gdi.h +6 -6
- data/test/helper.rb +0 -3
- data/test/test_bitmap.rb +31 -7
- data/test/test_bounds.rb +36 -0
- data/test/test_color.rb +59 -19
- data/test/test_color_space.rb +95 -0
- data/test/test_font.rb +5 -0
- data/test/test_image.rb +24 -20
- data/test/test_matrix.rb +106 -0
- data/test/test_painter.rb +157 -51
- data/test/test_painter_shape.rb +102 -0
- data/test/test_point.rb +29 -0
- data/test/test_polygon.rb +234 -0
- data/test/test_polygon_line.rb +167 -0
- data/test/test_polyline.rb +171 -0
- data/test/test_shader.rb +9 -9
- metadata +102 -70
- data/.doc/ext/rays/texture.cpp +0 -138
- data/ext/rays/texture.cpp +0 -149
- data/include/rays/ruby/texture.h +0 -41
- data/include/rays/texture.h +0 -71
- data/lib/rays/texture.rb +0 -24
- data/src/program.cpp +0 -648
- data/test/test_texture.rb +0 -27
@@ -0,0 +1,171 @@
|
|
1
|
+
#include "rays/ruby/camera.h"
|
2
|
+
|
3
|
+
|
4
|
+
#include "rays/ruby/image.h"
|
5
|
+
#include "defs.h"
|
6
|
+
|
7
|
+
|
8
|
+
RUCY_DEFINE_VALUE_FROM_TO(Rays::Camera)
|
9
|
+
|
10
|
+
#define THIS to<Rays::Camera*>(self)
|
11
|
+
|
12
|
+
#define CHECK RUCY_CHECK_OBJECT(Rays::Camera, self)
|
13
|
+
|
14
|
+
|
15
|
+
static
|
16
|
+
VALUE alloc(VALUE klass)
|
17
|
+
{
|
18
|
+
return new_type<Rays::Camera>(klass);
|
19
|
+
}
|
20
|
+
|
21
|
+
static
|
22
|
+
VALUE setup(VALUE self, VALUE device_name, VALUE min_width, VALUE min_height, VALUE resize, VALUE crop)
|
23
|
+
{
|
24
|
+
RUCY_CHECK_OBJ(Rays::Camera, self);
|
25
|
+
|
26
|
+
*THIS = Rays::Camera(
|
27
|
+
device_name ? to<const char*>(device_name) : NULL,
|
28
|
+
to<int>(min_width), to<int>(min_height),
|
29
|
+
to<bool>(resize), to<bool>(crop));
|
30
|
+
return self;
|
31
|
+
}
|
32
|
+
|
33
|
+
static
|
34
|
+
VALUE start(VALUE self)
|
35
|
+
{
|
36
|
+
CHECK;
|
37
|
+
return value(THIS->start());
|
38
|
+
}
|
39
|
+
|
40
|
+
static
|
41
|
+
VALUE stop(VALUE self)
|
42
|
+
{
|
43
|
+
CHECK;
|
44
|
+
THIS->stop();
|
45
|
+
}
|
46
|
+
|
47
|
+
static
|
48
|
+
VALUE is_active(VALUE self)
|
49
|
+
{
|
50
|
+
CHECK;
|
51
|
+
return value(THIS->is_active());
|
52
|
+
}
|
53
|
+
|
54
|
+
static
|
55
|
+
VALUE set_min_width(VALUE self, VALUE width)
|
56
|
+
{
|
57
|
+
CHECK;
|
58
|
+
THIS->set_min_width(to<int>(width));
|
59
|
+
return value(THIS->min_width());
|
60
|
+
}
|
61
|
+
|
62
|
+
static
|
63
|
+
VALUE min_width(VALUE self)
|
64
|
+
{
|
65
|
+
CHECK;
|
66
|
+
return value(THIS->min_width());
|
67
|
+
}
|
68
|
+
|
69
|
+
static
|
70
|
+
VALUE set_min_height(VALUE self, VALUE height)
|
71
|
+
{
|
72
|
+
CHECK;
|
73
|
+
THIS->set_min_height(to<int>(height));
|
74
|
+
return value(THIS->min_height());
|
75
|
+
}
|
76
|
+
|
77
|
+
static
|
78
|
+
VALUE min_height(VALUE self)
|
79
|
+
{
|
80
|
+
CHECK;
|
81
|
+
return value(THIS->min_height());
|
82
|
+
}
|
83
|
+
|
84
|
+
static
|
85
|
+
VALUE set_resize(VALUE self, VALUE resize)
|
86
|
+
{
|
87
|
+
CHECK;
|
88
|
+
THIS->set_resize(to<bool>(resize));
|
89
|
+
return value(THIS->is_resize());
|
90
|
+
}
|
91
|
+
|
92
|
+
static
|
93
|
+
VALUE is_resize(VALUE self)
|
94
|
+
{
|
95
|
+
CHECK;
|
96
|
+
return value(THIS->is_resize());
|
97
|
+
}
|
98
|
+
|
99
|
+
static
|
100
|
+
VALUE set_crop(VALUE self, VALUE crop)
|
101
|
+
{
|
102
|
+
CHECK;
|
103
|
+
THIS->set_crop(to<bool>(crop));
|
104
|
+
return value(THIS->is_crop());
|
105
|
+
}
|
106
|
+
|
107
|
+
static
|
108
|
+
VALUE is_crop(VALUE self)
|
109
|
+
{
|
110
|
+
CHECK;
|
111
|
+
return value(THIS->is_crop());
|
112
|
+
}
|
113
|
+
|
114
|
+
static
|
115
|
+
VALUE image(VALUE self)
|
116
|
+
{
|
117
|
+
CHECK;
|
118
|
+
const Rays::Image* img = THIS->image();
|
119
|
+
return img ? value(*img) : nil();
|
120
|
+
}
|
121
|
+
|
122
|
+
static
|
123
|
+
VALUE device_names(VALUE self)
|
124
|
+
{
|
125
|
+
auto names = Rays::get_camera_device_names();
|
126
|
+
|
127
|
+
std::vector<Value> v;
|
128
|
+
for (auto it = names.begin(), end = names.end(); it != end; ++it)
|
129
|
+
v.emplace_back(value(it->c_str()));
|
130
|
+
return value(v.size(), &v[0]);
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
static Class cCamera;
|
135
|
+
|
136
|
+
void
|
137
|
+
Init_camera ()
|
138
|
+
{
|
139
|
+
Module mRays = rb_define_module("Rays");
|
140
|
+
|
141
|
+
cCamera = rb_define_class_under(mRays, "Camera", rb_cObject);
|
142
|
+
rb_define_alloc_func(cCamera, alloc);
|
143
|
+
rb_define_private_method(cCamera, "setup", RUBY_METHOD_FUNC(setup), 5);
|
144
|
+
rb_define_method(cCamera, "start", RUBY_METHOD_FUNC(start), 0);
|
145
|
+
rb_define_method(cCamera, "stop", RUBY_METHOD_FUNC(stop), 0);
|
146
|
+
cCamera.define_method("active?", is_active);
|
147
|
+
rb_define_method(cCamera, "min_width=", RUBY_METHOD_FUNC(set_min_width), 1);
|
148
|
+
rb_define_method(cCamera, "min_width", RUBY_METHOD_FUNC(min_width), 0);
|
149
|
+
rb_define_method(cCamera, "min_height=", RUBY_METHOD_FUNC(set_min_height), 1);
|
150
|
+
rb_define_method(cCamera, "min_height", RUBY_METHOD_FUNC(min_height), 0);
|
151
|
+
rb_define_method(cCamera, "resize=", RUBY_METHOD_FUNC(set_resize), 1);
|
152
|
+
cCamera.define_method("resize?", is_resize);
|
153
|
+
rb_define_method(cCamera, "crop=", RUBY_METHOD_FUNC(set_crop), 1);
|
154
|
+
cCamera.define_method("crop?", is_crop);
|
155
|
+
rb_define_method(cCamera, "image", RUBY_METHOD_FUNC(image), 0);
|
156
|
+
rb_define_module_function(cCamera, "device_names", RUBY_METHOD_FUNC(device_names), 0);
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
namespace Rays
|
161
|
+
{
|
162
|
+
|
163
|
+
|
164
|
+
Class
|
165
|
+
camera_class ()
|
166
|
+
{
|
167
|
+
return cCamera;
|
168
|
+
}
|
169
|
+
|
170
|
+
|
171
|
+
}// Rays
|
data/.doc/ext/rays/color.cpp
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
#include "rays/ruby/color.h"
|
2
2
|
|
3
3
|
|
4
|
-
#include <
|
4
|
+
#include <map>
|
5
5
|
#include "defs.h"
|
6
6
|
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
RUCY_DEFINE_VALUE_FROM_TO(Rays::Color)
|
8
|
+
RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Rays::Color)
|
12
9
|
|
13
10
|
#define THIS to<Rays::Color*>(self)
|
14
11
|
|
@@ -22,34 +19,13 @@ VALUE alloc(VALUE klass)
|
|
22
19
|
}
|
23
20
|
|
24
21
|
static
|
25
|
-
VALUE
|
22
|
+
VALUE initialize(VALUE self)
|
26
23
|
{
|
27
24
|
CHECK;
|
28
25
|
check_arg_count(__FILE__, __LINE__, "Color#initialize", argc, 0, 1, 2, 3, 4);
|
29
26
|
|
30
|
-
if (argc
|
31
|
-
|
32
|
-
switch (argc)
|
33
|
-
{
|
34
|
-
case 1:
|
35
|
-
*THIS = Rays::Color(to<float>(argv[0]));
|
36
|
-
break;
|
37
|
-
|
38
|
-
case 2:
|
39
|
-
*THIS = Rays::Color(to<float>(argv[0]), to<float>(argv[1]));
|
40
|
-
break;
|
41
|
-
|
42
|
-
case 3:
|
43
|
-
*THIS = Rays::Color(
|
44
|
-
to<float>(argv[0]), to<float>(argv[1]), to<float>(argv[2]));
|
45
|
-
break;
|
46
|
-
|
47
|
-
case 4:
|
48
|
-
*THIS = Rays::Color(
|
49
|
-
to<float>(argv[0]), to<float>(argv[1]),
|
50
|
-
to<float>(argv[2]), to<float>(argv[3]));
|
51
|
-
break;
|
52
|
-
}
|
27
|
+
if (argc >= 1)
|
28
|
+
*THIS = to<Rays::Color>(argc, argv);
|
53
29
|
|
54
30
|
return self;
|
55
31
|
}
|
@@ -127,6 +103,82 @@ VALUE get_alpha(VALUE self)
|
|
127
103
|
}
|
128
104
|
|
129
105
|
|
106
|
+
typedef std::map<Rays::String, Rays::Color> ColorMap;
|
107
|
+
|
108
|
+
static ColorMap&
|
109
|
+
get_color_map ()
|
110
|
+
{
|
111
|
+
static ColorMap map;
|
112
|
+
if (map.empty())
|
113
|
+
{
|
114
|
+
map["no"] =
|
115
|
+
map["none"] =
|
116
|
+
map["transp"] =
|
117
|
+
map["transparent"] = Rays::gray(0, 0);
|
118
|
+
|
119
|
+
map["black"] = Rays::rgb8( 0, 0, 0);
|
120
|
+
map["white"] = Rays::rgb8(255, 241, 232);
|
121
|
+
map["gray"] =
|
122
|
+
map["lightgray"] = Rays::rgb8(194, 195, 199);
|
123
|
+
map["darkgray"] = Rays::rgb8( 95, 87, 79);
|
124
|
+
map["brown"] = Rays::rgb8(171, 82, 54);
|
125
|
+
map["red"] = Rays::rgb8(255, 0, 77);
|
126
|
+
map["orange"] = Rays::rgb8(255, 163, 0);
|
127
|
+
map["yellow"] = Rays::rgb8(255, 236, 39);
|
128
|
+
map["green"] = Rays::rgb8( 0, 228, 54);
|
129
|
+
map["darkgreen"] = Rays::rgb8( 0, 135, 81);
|
130
|
+
map["blue"] = Rays::rgb8( 41, 173, 255);
|
131
|
+
map["darkblue"] = Rays::rgb8( 29, 43, 83);
|
132
|
+
map["indigo"] = Rays::rgb8(131, 118, 156);
|
133
|
+
map["pink"] = Rays::rgb8(255, 119, 168);
|
134
|
+
map["peach"] = Rays::rgb8(255, 204, 170);
|
135
|
+
map["darkpurple"] = Rays::rgb8(126, 37, 83);
|
136
|
+
}
|
137
|
+
return map;
|
138
|
+
}
|
139
|
+
|
140
|
+
static const Rays::String
|
141
|
+
to_color_name (const char* name)
|
142
|
+
{
|
143
|
+
return Rays::String(name).downcase();
|
144
|
+
}
|
145
|
+
|
146
|
+
static const Rays::Color&
|
147
|
+
find_color (const char* name)
|
148
|
+
{
|
149
|
+
assert(name);
|
150
|
+
|
151
|
+
const ColorMap& map = get_color_map();
|
152
|
+
ColorMap::const_iterator it = map.find(to_color_name(name));
|
153
|
+
if (it == map.end())
|
154
|
+
argument_error(__FILE__, __LINE__, "color '%s' is not found.", name);
|
155
|
+
|
156
|
+
return it->second;
|
157
|
+
}
|
158
|
+
|
159
|
+
static
|
160
|
+
VALUE hsv(VALUE self)
|
161
|
+
{
|
162
|
+
check_arg_count(__FILE__, __LINE__, "Color.hsv", argc, 3, 4);
|
163
|
+
|
164
|
+
float h = to<float>(argv[0]);
|
165
|
+
float s = to<float>(argv[1]);
|
166
|
+
float v = to<float>(argv[2]);
|
167
|
+
if (argc >= 4)
|
168
|
+
return value(Rays::hsv(h, s, v, to<float>(argv[3])));
|
169
|
+
else
|
170
|
+
return value(Rays::hsv(h, s, v));
|
171
|
+
}
|
172
|
+
|
173
|
+
static
|
174
|
+
VALUE set_palette_color(VALUE self)
|
175
|
+
{
|
176
|
+
check_arg_count(__FILE__, __LINE__, "Color.set_palette_color", argc, 2, 3, 4, 5);
|
177
|
+
|
178
|
+
get_color_map()[to_color_name(argv[0].c_str())] = to<Rays::Color>(argc - 1, &argv[1]);
|
179
|
+
}
|
180
|
+
|
181
|
+
|
130
182
|
static Class cColor;
|
131
183
|
|
132
184
|
void
|
@@ -136,7 +188,7 @@ Init_color ()
|
|
136
188
|
|
137
189
|
cColor = rb_define_class_under(mRays, "Color", rb_cObject);
|
138
190
|
rb_define_alloc_func(cColor, alloc);
|
139
|
-
rb_define_private_method(cColor, "
|
191
|
+
rb_define_private_method(cColor, "initialize", RUBY_METHOD_FUNC(initialize), -1);
|
140
192
|
rb_define_private_method(cColor, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1);
|
141
193
|
rb_define_method(cColor, "red=", RUBY_METHOD_FUNC(set_red), 1);
|
142
194
|
rb_define_method(cColor, "red", RUBY_METHOD_FUNC(get_red), 0);
|
@@ -146,6 +198,8 @@ Init_color ()
|
|
146
198
|
rb_define_method(cColor, "blue", RUBY_METHOD_FUNC(get_blue), 0);
|
147
199
|
rb_define_method(cColor, "alpha=", RUBY_METHOD_FUNC(set_alpha), 1);
|
148
200
|
rb_define_method(cColor, "alpha", RUBY_METHOD_FUNC(get_alpha), 0);
|
201
|
+
rb_define_module_function(cColor, "hsv", RUBY_METHOD_FUNC(hsv), -1);
|
202
|
+
rb_define_module_function(cColor, "set_palette_color", RUBY_METHOD_FUNC(set_palette_color), -1);
|
149
203
|
}
|
150
204
|
|
151
205
|
|
@@ -153,31 +207,155 @@ namespace Rucy
|
|
153
207
|
{
|
154
208
|
|
155
209
|
|
210
|
+
static int
|
211
|
+
char2hex (char c)
|
212
|
+
{
|
213
|
+
if ('0' <= c && c <= '9') return c - '0';
|
214
|
+
else if ('a' <= c && c <= 'f') return 10 + c - 'a';
|
215
|
+
else if ('A' <= c && c <= 'F') return 10 + c - 'A';
|
216
|
+
else return -1;
|
217
|
+
}
|
218
|
+
|
219
|
+
static bool
|
220
|
+
parse_channel (float* channel, const char* str, size_t nchars, size_t index)
|
221
|
+
{
|
222
|
+
assert(channel && str && 1 <= nchars && nchars <= 2 && 0 <= index && index <= 3);
|
223
|
+
|
224
|
+
const char* p = str + index * nchars;
|
225
|
+
|
226
|
+
if (nchars == 1)
|
227
|
+
{
|
228
|
+
int c0 = char2hex(p[0]);
|
229
|
+
if (c0 < 0)
|
230
|
+
return false;
|
231
|
+
|
232
|
+
assert(c0 < 16);
|
233
|
+
|
234
|
+
*channel = c0 / 15.f;
|
235
|
+
return true;
|
236
|
+
}
|
237
|
+
else
|
238
|
+
{
|
239
|
+
int c0 = char2hex(p[0]);
|
240
|
+
int c1 = char2hex(p[1]);
|
241
|
+
if (c0 < 0 || c1 < 0)
|
242
|
+
return false;
|
243
|
+
|
244
|
+
assert(c0 < 16 && c1 < 16);
|
245
|
+
|
246
|
+
*channel = (c0 * 16 + c1) / 255.f;
|
247
|
+
return true;
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
static bool
|
252
|
+
parse_string (Rays::Color* color, const char* str)
|
253
|
+
{
|
254
|
+
assert(color && str);
|
255
|
+
|
256
|
+
if (*str != '#') return false;
|
257
|
+
++str;
|
258
|
+
|
259
|
+
size_t len = strlen(str);
|
260
|
+
switch (len)
|
261
|
+
{
|
262
|
+
case 3:
|
263
|
+
case 6:
|
264
|
+
{
|
265
|
+
size_t nchars = len / 3;
|
266
|
+
|
267
|
+
float r, g, b;
|
268
|
+
if (
|
269
|
+
!parse_channel(&r, str, nchars, 0) ||
|
270
|
+
!parse_channel(&g, str, nchars, 1) ||
|
271
|
+
!parse_channel(&b, str, nchars, 2))
|
272
|
+
{
|
273
|
+
return false;
|
274
|
+
}
|
275
|
+
|
276
|
+
color->reset(r, g, b);
|
277
|
+
break;
|
278
|
+
}
|
279
|
+
|
280
|
+
case 4:
|
281
|
+
case 8:
|
282
|
+
{
|
283
|
+
size_t nchars = len / 4;
|
284
|
+
|
285
|
+
float r, g, b, a;
|
286
|
+
if (
|
287
|
+
!parse_channel(&r, str, nchars, 0) ||
|
288
|
+
!parse_channel(&g, str, nchars, 1) ||
|
289
|
+
!parse_channel(&b, str, nchars, 2) ||
|
290
|
+
!parse_channel(&a, str, nchars, 3))
|
291
|
+
{
|
292
|
+
return false;
|
293
|
+
}
|
294
|
+
|
295
|
+
color->reset(r, g, b, a);
|
296
|
+
break;
|
297
|
+
}
|
298
|
+
|
299
|
+
default: return false;
|
300
|
+
}
|
301
|
+
|
302
|
+
return true;
|
303
|
+
}
|
304
|
+
|
305
|
+
static Rays::Color
|
306
|
+
str2color (const char* str)
|
307
|
+
{
|
308
|
+
if (!str)
|
309
|
+
argument_error(__FILE__, __LINE__);
|
310
|
+
|
311
|
+
Rays::String str_ = str;
|
312
|
+
str_ = str_.strip();
|
313
|
+
|
314
|
+
Rays::Color color;
|
315
|
+
if (parse_string(&color, str_.c_str()))
|
316
|
+
return color;
|
317
|
+
else
|
318
|
+
return find_color(str_.c_str());
|
319
|
+
}
|
320
|
+
|
156
321
|
template <> Rays::Color
|
157
|
-
value_to<Rays::Color> (
|
322
|
+
value_to<Rays::Color> (int argc, const Value*argv, bool convert)
|
158
323
|
{
|
324
|
+
if (argc == 1 && argv->is_array())
|
325
|
+
{
|
326
|
+
argc = argv->size();
|
327
|
+
argv = argv->as_array();
|
328
|
+
}
|
329
|
+
|
330
|
+
assert(argc == 0 || (argc > 0 && argv));
|
331
|
+
|
159
332
|
if (convert)
|
160
333
|
{
|
161
|
-
if (
|
162
|
-
return Rays::Color(
|
163
|
-
else if (
|
334
|
+
if (argc == 0)
|
335
|
+
return Rays::Color();
|
336
|
+
else if (argv->is_nil())
|
337
|
+
return str2color("none");
|
338
|
+
else if (argv->is_s() || argv->is_sym())
|
339
|
+
return str2color(argv[0].c_str());
|
340
|
+
else if (argv->is_num())
|
164
341
|
{
|
165
|
-
|
166
|
-
if (size <= 0 || 4 < size)
|
167
|
-
Rucy::argument_error(__FILE__, __LINE__);
|
168
|
-
|
169
|
-
Value* a = value.as_array();
|
170
|
-
switch (size)
|
342
|
+
switch (argc)
|
171
343
|
{
|
172
|
-
|
173
|
-
case
|
174
|
-
case
|
175
|
-
case
|
344
|
+
#define V(i) argv[i].as_f(true)
|
345
|
+
case 1: return Rays::Color(V(0));
|
346
|
+
case 2: return Rays::Color(V(0), V(1));
|
347
|
+
case 3: return Rays::Color(V(0), V(1), V(2));
|
348
|
+
case 4: return Rays::Color(V(0), V(1), V(2), V(3));
|
349
|
+
#undef V
|
350
|
+
default: argument_error(__FILE__, __LINE__, "invalid array size.");
|
176
351
|
}
|
177
352
|
}
|
178
353
|
}
|
179
354
|
|
180
|
-
|
355
|
+
if (argc != 1)
|
356
|
+
argument_error(__FILE__, __LINE__);
|
357
|
+
|
358
|
+
return value_to<Rays::Color&>(*argv, convert);
|
181
359
|
}
|
182
360
|
|
183
361
|
|