Color class for interaction with colors
Methods
Public Class
Public Instance
Attributes
Public Class methods
Create new Color object from given value which can be of following types:
String |
Any color representation of Xlib is allowed |
Array |
Must be an array with values for red, green and blue |
Hash |
Must be a hash with values for red, green and blue |
Fixnum |
Pixel representation of a color in Xlib |
Color |
Copy color from a Color object |
Or just pass one argument for red, green or blue.
color = Subtlext::Color.new(51, 102, 253) => #<Subtlext::Color:xxx> color = Subtlext::Color.new("#336699") => #<Subtlext::Color:xxx> color = Subtlext::Color.new("red") => #<Subtlext::Color:xxx> color = Subtlext::Color.new([ 51, 102, 253 ]) => #<Subtlext::Color:xxx> color = Subtlext::Color.new({ :red => 51, :green => 102, :blue => 253 ]) => #<Subtlext::Color:xxx> color = Subtlext::Color.new(14253553) => #<Subtlext::Color:xxx> color = Subtlext::Color.new(Subtlext::Color.new(51, 102, 253)) => #<Subtlext::Color:xxx>
VALUE subextColorInit(int argc, VALUE *argv, VALUE self) { VALUE data[3] = { Qnil }; XColor xcolor = { 0 }; rb_scan_args(argc, argv, "12", &data[0], &data[1], &data[2]); subextSubtlextConnect(NULL); ///< Implicit open connection /* Get color values */ subextColorPixel(data[0], data[1], data[2], &xcolor); /* Set values */ rb_iv_set(self, "@red", INT2FIX(xcolor.red)); rb_iv_set(self, "@green", INT2FIX(xcolor.green)); rb_iv_set(self, "@blue", INT2FIX(xcolor.blue)); rb_iv_set(self, "@pixel", LONG2NUM(xcolor.pixel)); return self; }
Public Instance methods
Convert this Color to string and concat given string.
color + "subtle" => "<>123456789<>subtle"
VALUE subextColorOperatorPlus(VALUE self, VALUE value) { return subextSubtlextConcat(subextColorToString(self), value); }
Whether both objects have the same value. Returns -1, 0 or 1 when self is less than, equal to or grater than other. (based on pixel)
object1 <=> object2 => 0
static VALUE SubtlextEqualSpacePixel(VALUE self, VALUE other) { return SubtlextSpaceship(self, other, "@pixel"); }
Whether both objects have the same values (based on pixel).
object1 == object2 => true
VALUE subextColorEqual(VALUE self, VALUE other) { return ColorEqual(self, other, False); }
Whether both objects have the same values and types (based on pixel).
object1.eql? object2 => true
VALUE subextColorEqualTyped(VALUE self, VALUE other) { return ColorEqual(self, other, True); }
Convert this object to hash.
puts object.hash => 1746246187916025425
static VALUE SubtlextHash(VALUE self) { VALUE str = Qnil, id = rb_intern("to_str"); /* Convert to string */ if(rb_respond_to(self, id)) str = rb_funcall(self, id, 0, Qnil); return T_STRING == rb_type(str) ? INT2FIX(rb_str_hash(str)) : Qnil; }
Convert this Color object to an array with one fixnum for red, blue and green.
color.to_a => [ 51, 102, 253 ]
VALUE subextColorToArray(VALUE self) { VALUE ary = Qnil, red = Qnil, green = Qnil, blue = Qnil; /* Check ruby object */ GET_ATTR(self, "@red", red); GET_ATTR(self, "@green", green); GET_ATTR(self, "@blue", blue); /* Create new array */ ary = rb_ary_new2(3); /* Set values */ rb_ary_push(ary, red); rb_ary_push(ary, green); rb_ary_push(ary, blue); return ary; }
Convert this Color object to a hash with one symbol/fixnum pair for red, green and blue.
color.to_hash => { :red => 51, :green => 102, :blue => 253 }
VALUE subextColorToHash(VALUE self) { VALUE klass = Qnil, hash = Qnil, red = Qnil, green = Qnil, blue = Qnil; /* Check ruby object */ GET_ATTR(self, "@red", red); GET_ATTR(self, "@green", green); GET_ATTR(self, "@blue", blue); /* Create new hash */ klass = rb_const_get(rb_mKernel, rb_intern("Hash")); hash = rb_funcall(klass, rb_intern("new"), 0, NULL); /* Set values */ rb_hash_aset(hash, CHAR2SYM("red"), red); rb_hash_aset(hash, CHAR2SYM("green"), green); rb_hash_aset(hash, CHAR2SYM("blue"), blue); return hash; }
Convert this Color object to rrggbb hex string.
puts color.to_hex => "#ff0000"
VALUE subextColorToHex(VALUE self) { char buf[8] = { 0 }; VALUE red = Qnil, green = Qnil, blue = Qnil; /* Check ruby object */ GET_ATTR(self, "@red", red); GET_ATTR(self, "@green", green); GET_ATTR(self, "@blue", blue); snprintf(buf, sizeof(buf), "#%02X%02X%02X", (int)FIX2INT(red), (int)FIX2INT(green), (int)FIX2INT(blue)); return rb_str_new2(buf); }
Convert this Color object to string.
puts color => "<>123456789<>"
VALUE subextColorToString(VALUE self) { char buf[20] = { 0 }; VALUE pixel = Qnil; /* Check ruby object */ GET_ATTR(self, "@pixel", pixel); snprintf(buf, sizeof(buf), "%s#%ld%s", SEPARATOR, NUM2LONG(pixel), SEPARATOR); return rb_str_new2(buf); }