Class: RedBird::Palette

Inherits:
Data
  • Object
show all
Defined in:
ext/red_bird/palette.c,
lib/red_bird/palette.rb,
ext/red_bird/palette.c

Overview

A palette consists in a set of 256 colors.

Author:

  • Frederico Linhares

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colors) ⇒ Object

Parameters:

Author:

  • Frederico Linhares



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/red_bird/palette.c', line 53

VALUE
bird_cPalette_initialize(VALUE self, VALUE colors)
{
  long ary_index, ary_len;
  VALUE v_color;
  struct bird_palette_data *ptr;
  const char* error_message =
      "colors must be an Array with 256 instances of RedBird::Color";

  if(!rb_obj_is_kind_of(colors, rb_cArray))
    rb_raise(rb_eArgError, "%s", error_message);

  ary_len = RARRAY_LEN(colors);
  if(ary_len != 256)
    rb_raise(rb_eArgError, "%s", error_message);

  TypedData_Get_Struct(self, struct bird_palette_data, &bird_palette_type,
                       ptr);

  for(ary_index = 0; ary_index < ary_len; ary_index++)
  {
    v_color = rb_ary_entry(colors, ary_index);
    if(!rb_obj_is_kind_of(v_color, bird_cColor))
      rb_raise(rb_eArgError, "%s", error_message);

    ptr->colors[ary_index] = bird_cColor_get_data(v_color)->data;
  }

  return self;
}

Class Method Details

.from_yml(palette_file) ⇒ RedBird::Palette

Load information from a YAML file and uses it to create an instance of this class

Parameters:

  • tile_file (String)

    path to the yaml file.

Returns:

Author:

  • Frederico Linhares



12
13
14
15
16
17
18
19
20
21
# File 'lib/red_bird/palette.rb', line 12

def self.from_yml(palette_file)
  data = YAML.load_file(palette_file)

  # Convert data from YAML into Color instances.
  colors = data.map do |i|
    Color.new(i[0], i[1], i[2], i[3])
  end

  return self.new(colors)
end

Instance Method Details

#get_color(index) ⇒ RedBird::Color

Returns a instance of Color with values referenced by the index.

Parameters:

  • index (Integer)

    color index. Must be between 0 and 255.

Returns:

Author:

  • Frederico Linhares



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/red_bird/palette.c', line 90

VALUE
bird_cPalette_get_color(VALUE self, VALUE index)
{
  VALUE argv[4];
  int c_index;
  struct bird_palette_data *ptr;

  RB_INTEGER_TYPE_P(index);

  c_index = NUM2INT(index);
  if(c_index < 0 || c_index > 255)
    rb_raise(rb_eArgError, "%s", "index must be between 0 and 255");

  TypedData_Get_Struct(self, struct bird_palette_data, &bird_palette_type,
                       ptr);

  argv[0] = UINT2NUM(ptr->colors[c_index].r);
  argv[1] = UINT2NUM(ptr->colors[c_index].g);
  argv[2] = UINT2NUM(ptr->colors[c_index].b);
  argv[3] = UINT2NUM(ptr->colors[c_index].a);

  return rb_class_new_instance(4, argv, bird_cColor);
}