#include "rays/ruby/color.h" #include #include "defs.h" using namespace Rucy; static Class cColor; namespace Rays { Class color_class () { return cColor; } }// Rays namespace Rucy { Value value (const Rays::Color& obj) { return new_type(cColor, new Rays::Color(obj)); } Value value (const Rays::Color* obj) { return obj ? value(*obj) : nil(); } }// Rucy #define THIS to(self) #define CHECK RUCY_CHECK_OBJ(self, Rays::Color, cColor) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE initialize(VALUE self) { RUCY_CHECK_OBJ(self, Rays::Color, cColor); if (argc < 0 || 4 < argc) arg_count_error("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) { RUCY_CHECK_OBJ(self, Rays::Color, cColor); Rays::Color* color = to(obj); if (!color) argument_error(); *THIS = *color; 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, "initialize", RUBY_METHOD_FUNC(initialize), -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); }