#include "rays/ruby/color.h" #include #include "defs.h" using namespace Rucy; static Class cColor; RUCY_DEFINE_VALUE_FROM_TO(Rays::Color, cColor) #define THIS to(self) #define CHECK RUCY_CHECK_OBJ(Rays::Color, cColor, self) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE setup(VALUE self) { CHECK; check_arg_count(__FILE__, __LINE__, "Color#initialize", argc, 0, 1, 2, 3, 4); if (argc == 0) return self; switch (argc) { case 1: *THIS = Rays::Color(to(argv[0])); break; case 2: *THIS = Rays::Color(to(argv[0]), to(argv[1])); break; case 3: *THIS = Rays::Color( to(argv[0]), to(argv[1]), to(argv[2])); break; case 4: *THIS = Rays::Color( to(argv[0]), to(argv[1]), to(argv[2]), to(argv[3])); break; } return self; } static VALUE initialize_copy(VALUE self, VALUE obj) { CHECK; *THIS = to(obj); return self; } static VALUE set_red(VALUE self, VALUE red) { CHECK; return value(THIS->red = to(red)); } static VALUE get_red(VALUE self) { CHECK; return value(THIS->red); } static VALUE set_green(VALUE self, VALUE green) { CHECK; return value(THIS->green = to(green)); } static VALUE get_green(VALUE self) { CHECK; return value(THIS->green); } static VALUE set_blue(VALUE self, VALUE blue) { CHECK; return value(THIS->blue = to(blue)); } static VALUE get_blue(VALUE self) { CHECK; return value(THIS->blue); } static VALUE set_alpha(VALUE self, VALUE alpha) { CHECK; return value(THIS->alpha = to(alpha)); } static VALUE get_alpha(VALUE self) { CHECK; return value(THIS->alpha); } void Init_color () { Module mRays = rb_define_module("Rays"); cColor = rb_define_class_under(mRays, "Color", rb_cObject); rb_define_alloc_func(cColor, alloc); rb_define_private_method(cColor, "setup", RUBY_METHOD_FUNC(setup), -1); rb_define_private_method(cColor, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1); rb_define_method(cColor, "red=", RUBY_METHOD_FUNC(set_red), 1); rb_define_method(cColor, "red", RUBY_METHOD_FUNC(get_red), 0); rb_define_method(cColor, "green=", RUBY_METHOD_FUNC(set_green), 1); rb_define_method(cColor, "green", RUBY_METHOD_FUNC(get_green), 0); rb_define_method(cColor, "blue=", RUBY_METHOD_FUNC(set_blue), 1); rb_define_method(cColor, "blue", RUBY_METHOD_FUNC(get_blue), 0); rb_define_method(cColor, "alpha=", RUBY_METHOD_FUNC(set_alpha), 1); rb_define_method(cColor, "alpha", RUBY_METHOD_FUNC(get_alpha), 0); } namespace Rucy { template <> Rays::Color value_to (Value value, bool convert) { if (convert) { if (value.is_i() || value.is_f()) return Rays::Color(value.as_f(true)); else if (value.is_array()) { int size = value.size(); if (size <= 0 || 4 < size) Rucy::argument_error(__FILE__, __LINE__); Value* a = value.as_array(); switch (size) { case 1: return Rays::Color(a[0].as_f(true)); case 2: return Rays::Color(a[0].as_f(true), a[1].as_f(true)); case 3: return Rays::Color(a[0].as_f(true), a[1].as_f(true), a[2].as_f(true)); case 4: return Rays::Color(a[0].as_f(true), a[1].as_f(true), a[2].as_f(true), a[3].as_f(true)); } } } return value_to(value, convert); } }// Rucy namespace Rays { Class color_class () { return cColor; } }// Rays