sfml3.rb

Class: SFML::Color

Inherits:
Object
  • Object
show all
Defined in:
ext/graphics/color.c,
ext/graphics/color.c

Overview

An RGBA color, each channel an Integer 0..255.

Constant Summary collapse

BLACK =

Opaque black, RGBA(0, 0, 0, 255).

rb_obj_freeze(Color_wrap(sfBlack))
WHITE =

Opaque white, RGBA(255, 255, 255, 255).

rb_obj_freeze(Color_wrap(sfWhite))
RED =

Opaque red, RGBA(255, 0, 0, 255).

rb_obj_freeze(Color_wrap(sfRed))
GREEN =

Opaque green, RGBA(0, 255, 0, 255).

rb_obj_freeze(Color_wrap(sfGreen))
BLUE =

Opaque blue, RGBA(0, 0, 255, 255).

rb_obj_freeze(Color_wrap(sfBlue))
YELLOW =

Opaque yellow, RGBA(255, 255, 0, 255).

rb_obj_freeze(Color_wrap(sfYellow))
MAGENTA =

Opaque magenta, RGBA(255, 0, 255, 255).

rb_obj_freeze(Color_wrap(sfMagenta))
CYAN =

Opaque cyan, RGBA(0, 255, 255, 255).

rb_obj_freeze(Color_wrap(sfCyan))
TRANSPARENT =

Fully transparent black, RGBA(0, 0, 0, 0).

rb_obj_freeze(Color_wrap(sfTransparent))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ Color(0, 0, 0, 255) #new(r, g, b, a = 255) ⇒ Color(r] #new([r, g, b]) ⇒ Color(r, 255) #new([r, g, b, a]) ⇒ Color(r] #new(packed_integer) ⇒ Color #new(other_color) ⇒ Color

Each channel argument is clamped to 0..255. A single Integer argument is interpreted as #from_rgb (0xRRGGBB) if it fits in 24 bits, or #from_rgba (0xRRGGBBAA) otherwise.

Overloads:

  • #new ⇒ Color(0, 0, 0, 255)
  • #new(r, g, b, a = 255) ⇒ Color(r]

    Returns Color(r].

  • #new([r, g, b]) ⇒ Color(r, 255)
  • #new([r, g, b, a]) ⇒ Color(r]

    Returns Color(r].

  • #new(packed_integer) ⇒ Color

Raises:

  • (ArgumentError) —

    if given an Array shorter than 3 elements, or an argument count other than 0, 1, 3 or 4



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/graphics/color.c', line 85

static VALUE Color_initialize(int argc, VALUE* argv, VALUE self) {
    sfColor color = {0, 0, 0, 255};

    if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cColor)) {
        color = ((Color*)Get_Color_Struct(argv[0]))->color;
    } else if (argc == 1 && RB_TYPE_P(argv[0], T_ARRAY)) {
        color = color_from_rb(argv[0]);
    } else if (argc == 1 && RB_INTEGER_TYPE_P(argv[0])) {
        color = Color_from_integer(argv[0]);
    } else if (argc == 3 || argc == 4) {
        color.r = clamp_channel(NUM2INT(argv[0]));
        color.g = clamp_channel(NUM2INT(argv[1]));
        color.b = clamp_channel(NUM2INT(argv[2]));
        color.a = (argc == 4) ? clamp_channel(NUM2INT(argv[3])) : 255;
    } else if (argc != 0) {
        raise_invalid_arguments_excepted(4, argc);
    }

    ((Color*)Get_Color_Struct(self))->color = color;

    return self;
}

Instance Attribute Details

#a ⇒ Integer

Returns the alpha component.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the alpha channel, 0..255



335
336
337
# File 'ext/graphics/color.c', line 335

static VALUE Color_get_a(VALUE self) {
    return INT2NUM(((Color*)Get_Color_Struct(self))->color.a);
}

#b ⇒ Integer

Returns the blue component.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the blue channel, 0..255



335
336
337
# File 'ext/graphics/color.c', line 335

static VALUE Color_get_b(VALUE self) {
    return INT2NUM(((Color*)Get_Color_Struct(self))->color.b);
}

#g ⇒ Integer

Returns the green component.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the green channel, 0..255



335
336
337
# File 'ext/graphics/color.c', line 335

static VALUE Color_get_g(VALUE self) {
    return INT2NUM(((Color*)Get_Color_Struct(self))->color.g);
}

#r ⇒ Integer

Returns the red component.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the red channel, 0..255



335
336
337
# File 'ext/graphics/color.c', line 335

static VALUE Color_get_r(VALUE self) {
    return INT2NUM(((Color*)Get_Color_Struct(self))->color.r);
}

Class Method Details

.from_rgba(value) ⇒ Color

Also available as .from_integer.

Returns:

Returns:

  • (Color) —

    the color packed as 0xRRGGBBAA (or 0xRRGGBB, treated as fully opaque, if it fits in 24 bits)



129
130
131
# File 'ext/graphics/color.c', line 129

static VALUE Color_from_rgba(VALUE klass, VALUE rb_integer) {
    return Color_wrap(Color_from_integer(rb_integer));
}

.from_rgb(value) ⇒ Color

Creates a color from 8-bit red, green and blue components.

Returns:

Returns:

  • (Color) —

    the fully-opaque color packed as 0xRRGGBB



115
116
117
118
119
# File 'ext/graphics/color.c', line 115

static VALUE Color_from_rgb(VALUE klass, VALUE rb_integer) {
    return Color_wrap((sfColor){(unsigned char)((NUM2UINT(rb_integer) >> 16) & 0xFF),
                                (unsigned char)((NUM2UINT(rb_integer) >> 8) & 0xFF),
                                (unsigned char)(NUM2UINT(rb_integer) & 0xFF), 255});
}

.from_rgba(value) ⇒ Color

Also available as .from_integer.

Returns:

Returns:

  • (Color) —

    the color packed as 0xRRGGBBAA (or 0xRRGGBB, treated as fully opaque, if it fits in 24 bits)



129
130
131
# File 'ext/graphics/color.c', line 129

static VALUE Color_from_rgba(VALUE klass, VALUE rb_integer) {
    return Color_wrap(Color_from_integer(rb_integer));
}

Instance Method Details

#*(other) ⇒ Color

Componentwise modulation: each channel is +(self_channel * other_channel) / 255+. other may be a Color, a 3/4-element Array, or a packed Integer.

Returns:

Returns:



311
312
313
314
315
316
317
318
# File 'ext/graphics/color.c', line 311

static VALUE Color_mul(VALUE self, VALUE rb_other) {
    sfColor a = ((Color*)Get_Color_Struct(self))->color;
    sfColor b = color_from_rb(rb_other);

    return Color_wrap(
        (sfColor){(unsigned char)((a.r * b.r) / 255), (unsigned char)((a.g * b.g) / 255),
                  (unsigned char)((a.b * b.b) / 255), (unsigned char)((a.a * b.a) / 255)});
}

#+(other) ⇒ Color

other may be a Color, a 3/4-element Array, or a packed Integer. Channels are summed and clamped to 0..255.

Returns:

Returns:



279
280
281
282
283
284
285
# File 'ext/graphics/color.c', line 279

static VALUE Color_add(VALUE self, VALUE rb_other) {
    sfColor a = ((Color*)Get_Color_Struct(self))->color;
    sfColor b = color_from_rb(rb_other);

    return Color_wrap((sfColor){clamp_channel(a.r + b.r), clamp_channel(a.g + b.g),
                                clamp_channel(a.b + b.b), clamp_channel(a.a + b.a)});
}

#-(other) ⇒ Color

other may be a Color, a 3/4-element Array, or a packed Integer. Channels are subtracted and clamped to 0..255.

Returns:

Returns:



295
296
297
298
299
300
301
# File 'ext/graphics/color.c', line 295

static VALUE Color_sub(VALUE self, VALUE rb_other) {
    sfColor a = ((Color*)Get_Color_Struct(self))->color;
    sfColor b = color_from_rb(rb_other);

    return Color_wrap((sfColor){clamp_channel(a.r - b.r), clamp_channel(a.g - b.g),
                                clamp_channel(a.b - b.b), clamp_channel(a.a - b.a)});
}

#==(other) ⇒ Boolean

Returns true if the two objects are equal.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/graphics/color.c', line 258

static VALUE Color_eql(VALUE self, VALUE rb_other) {
    sfColor a = ((Color*)Get_Color_Struct(self))->color;
    sfColor b;

    if (!rb_obj_is_kind_of(rb_other, rb_cColor)) {
        return Qfalse;
    }

    b = ((Color*)Get_Color_Struct(rb_other))->color;

    return BOOL2RB(a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
}

#to_a ⇒ Array, Integer

Returns the object as an Array.

Returns ].

Returns:

  • (Array, Integer, Integer, Integer) —

    ]

Returns:

  • (Array<Integer>) —

    [r, g, b, a]



244
245
246
247
248
249
# File 'ext/graphics/color.c', line 244

static VALUE Color_to_a(VALUE self) {
    sfColor color = ((Color*)Get_Color_Struct(self))->color;

    return rb_ary_new_from_args(4, INT2NUM(color.r), INT2NUM(color.g), INT2NUM(color.b),
                                INT2NUM(color.a));
}

#to_a ⇒ Array, Integer

Returns the object as an Array.

Returns ].

Returns:

  • (Array, Integer, Integer, Integer) —

    ]

Returns:

  • (Array<Integer>) —

    [r, g, b, a]



244
245
246
247
248
249
# File 'ext/graphics/color.c', line 244

static VALUE Color_to_a(VALUE self) {
    sfColor color = ((Color*)Get_Color_Struct(self))->color;

    return rb_ary_new_from_args(4, INT2NUM(color.r), INT2NUM(color.g), INT2NUM(color.b),
                                INT2NUM(color.a));
}

#to_i ⇒ Integer

Returns the color encoded as a 32-bit integer.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the color packed as 0xRRGGBBAA



231
232
233
234
235
236
# File 'ext/graphics/color.c', line 231

static VALUE Color_to_i(VALUE self) {
    sfColor color = ((Color*)Get_Color_Struct(self))->color;

    return UINT2NUM(((unsigned int)color.r << 24) | ((unsigned int)color.g << 16) |
                    ((unsigned int)color.b << 8) | (unsigned int)color.a);
}

#to_s ⇒ String

Returns a human-readable representation of the object.

Returns:

  • (String)

Returns:

  • (String) —

    "(r, g, b, a)"



326
327
328
329
330
331
332
333
# File 'ext/graphics/color.c', line 326

static VALUE Color_to_s(VALUE self) {
    sfColor c = ((Color*)Get_Color_Struct(self))->color;
    char buffer[64];

    snprintf(buffer, sizeof(buffer), "(%u, %u, %u, %u)", c.r, c.g, c.b, c.a);

    return rb_str_new2(buffer);
}