Class Subtlext::Color < Object

Color class for interaction with colors

Methods

public class

  1. new

public instance

  1. +
  2. <=>
  3. ==
  4. eql?
  5. hash
  6. to_a
  7. to_a
  8. to_h
  9. to_hash
  10. to_hex
  11. to_s
  12. to_str

Attributes

blue [R] Blue fraction
green [R] Green fraction
pixel [R] Pixel number
red [R] Red fraction

Public class methods

new(red, green, blue) → Subtlext::Color
new(string) → Subtlext::Color
new(array) → Subtlext::Color
new(hash) → Subtlext::Color
new(fixnum) → Subtlext::Color
new(color) → Subtlext::Color

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.

 1 color = Subtlext::Color.new(51, 102, 253)
 2 => #<Subtlext::Color:xxx>
 3 
 4 color = Subtlext::Color.new("#336699")
 5 => #<Subtlext::Color:xxx>
 6 
 7 color = Subtlext::Color.new("red")
 8 => #<Subtlext::Color:xxx>
 9 
10 color = Subtlext::Color.new([ 51, 102, 253 ])
11 => #<Subtlext::Color:xxx>
12 
13 color = Subtlext::Color.new({ :red => 51, :green => 102, :blue => 253 ])
14 => #<Subtlext::Color:xxx>
15 
16 color = Subtlext::Color.new(14253553)
17 => #<Subtlext::Color:xxx>
18 
19 color = Subtlext::Color.new(Subtlext::Color.new(51, 102, 253))
20 => #<Subtlext::Color:xxx>
[show source]
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

+(string) → String

Convert this Color to string and concat given string.

1 color + "subtle"
2 => "<>123456789<>subtle"
[show source]
VALUE
subextColorOperatorPlus(VALUE self,
  VALUE value)
{
  return subextSubtlextConcat(subextColorToString(self), value);
}
<=>(other) → -1, 0 or 1

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)

1 object1 <=> object2
2 => 0
[show source]
static VALUE
SubtlextEqualSpacePixel(VALUE self,
  VALUE other)
{
  return SubtlextSpaceship(self, other, "@pixel");
}
==(other) → True or False

Whether both objects have the same values (based on pixel).

1 object1 == object2
2 => true
[show source]
VALUE
subextColorEqual(VALUE self,
  VALUE other)
{
  return ColorEqual(self, other, False);
}
eql?(other) → True or False

Whether both objects have the same values and types (based on pixel).

1 object1.eql? object2
2 => true
[show source]
VALUE
subextColorEqualTyped(VALUE self,
  VALUE other)
{
  return ColorEqual(self, other, True);
}
hash → Hash

Convert this object to hash.

1 puts object.hash
2 => 1746246187916025425
[show source]
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;
}
to_a ()

Alias for to_ary

to_a → Array

Convert this Color object to an array with one fixnum for red, blue and green.

1 color.to_a
2 => [ 51, 102, 253 ]
[show source]
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;
}
to_h ()

Alias for to_hash

to_hash → Hash

Convert this Color object to a hash with one symbol/fixnum pair for red, green and blue.

1 color.to_hash
2 => { :red => 51, :green => 102, :blue => 253 }
[show source]
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;
}
to_hex → String

Convert this Color object to rrggbb hex string.

1 puts color.to_hex
2 => "#ff0000"
[show source]
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);
}
to_s ()

Alias for to_str

to_str → String

Convert this Color object to string.

1 puts color
2 => "<>123456789<>"
[show source]
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);
}