class Subtlext::Color

  1. ../../../../../tmp/subtlext.c
Superclass: Object

Color class for interaction with colors

Methods

Public Class

  1. new

Public Instance

  1. +
  2. <=>
  3. ==
  4. blue
  5. eql?
  6. green
  7. hash
  8. pixel
  9. red
  10. to_ary
  11. to_hash
  12. to_hex
  13. to_str

Public Instance Aliases

to_a -> to_ary

Aliases

to_h -> to_hash
to_s -> 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.

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>
[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.

color + "subtle"
=> "<>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)

object1 <=> object2
=> 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).

object1 == object2
=> 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).

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

Convert this object to hash.

puts object.hash
=> 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 -> Array

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

color.to_a
=> [ 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_hash -> Hash

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 }
[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.

puts color.to_hex
=> "#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_str -> String

Convert this Color object to string.

puts color
=> "<>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);
}