Changeset 120

Show
Ignore:
Timestamp:
08/19/2007 17:59:05 (13 months ago)
Author:
why
Message:

* shoes/ruby.c: okay, using the Color class for everywhere colors are put to use.
* samples/: switched most of the examples to using the rgb and gray methods, though the old techniques are still good.

Location:
trunk
Files:
13 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/shoes.rb

    r115 r120  
    99class Range  
    1010  def rand  
    11     (Kernel.rand * (self.end - self.begin)) + self.begin  
     11    conv = (self.end === Integer && self.begin === Integer ? :to_i : :to_f) 
     12    ((Kernel.rand * (self.end - self.begin)) + self.begin).send(conv)  
    1213  end  
    1314end 
  • trunk/samples/anim-move.rb

    r97 r120  
    11Shoes.app do 
    2   background "rgb(0, 0, 0)" 
    3   fill 1.0, 1.0, 1.0 
     2  background rgb(0, 0, 0) 
     3  fill rgb(255, 255, 255) 
    44  rects = [ 
    55    rect(0, 0, 50, 50), 
  • trunk/samples/bounce.rb

    r115 r120  
    88  animate(30) do 
    99    clear do 
    10       background "rgb(102, 102, 102)" 
     10      background rgb(102, 102, 102) 
    1111      x += xspeed * xdir 
    1212      y += yspeed * ydir 
  • trunk/samples/calc.rb

    r85 r120  
    4242 
    4343  stack :margin => 4 do 
    44     background "rgb(240, 240, 210)", :radius => 5 
     44    background rgb(240, 240, 210), :radius => 5 
    4545 
    4646    stack do 
  • trunk/samples/draw.rb

    r113 r120  
    11Shoes.app do 
    2   background "rgb(102, 102, 102)" 
    3   stroke 0, 0, 0 
     2  background "#999" 
     3  stroke "#000" 
    44  x, y = nil, nil 
    55  motion do |_x, _y| 
  • trunk/samples/edit.rb

    r27 r120  
    11str, t = "", nil 
    22Shoes.app :height => 500, :width => 450 do 
    3   background "rgb(77, 77, 77)" 
     3  background rgb(77, 77, 77) 
    44  stack :margin => 10 do 
    55    text "<span color='white'><span color='red' background='white'>TEXT EDITOR</span> * USE ALT-Q TO QUIT</span>" 
  • trunk/samples/follow.rb

    r108 r120  
    22Shoes.app do 
    33  nostroke 
    4   fill 1.0, 1.0, 1.0, 0.6 
     4  fill rgb(0x30, 0xFF, 0xFF, 0.6) 
    55  animate(24) do 
    66    trails.shift 
     
    88 
    99    clear do 
    10       background "rgb(51, 51, 51)" 
     10      background rgb(51, 51, 51) 
    1111      trails.each_with_index do |(x, y), i| 
    1212        i += 1 
  • trunk/samples/rect.rb

    r86 r120  
    22  20.times do 
    33    nostroke 
    4     fill (0.6..1.0).rand, (0.1..1.0).rand, (0.2..1.0).rand, (0.4..1.0).rand 
     4    fill rgb((0.6..1.0).rand, (0.1..1.0).rand, (0.2..1.0).rand, (0.4..1.0).rand) 
    55    r = rand(300) + 60 
    66    rect (10..100).rand, (10..200).rand, r, r 
  • trunk/samples/timer.rb

    r12 r120  
    11label, time = nil, Time.now 
    22Shoes.app :height => 150, :width => 250 do 
    3   background "rgb(240, 250, 208)" 
     3  background rgb(240, 250, 208) 
    44  stack :margin => 10 do 
    55    button "Start" do 
  • trunk/shoes/canvas.c

    r115 r120  
    240240  canvas->cr = cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1));; 
    241241  canvas->sw = 1.; 
    242   canvas->fg.r = 0.; 
    243   canvas->fg.g = 0.; 
    244   canvas->fg.b = 0.; 
    245   canvas->fg.a = 1.; 
     242  canvas->fg.r = 0x00; 
     243  canvas->fg.g = 0x00; 
     244  canvas->fg.b = 0x00; 
     245  canvas->fg.a = 0xFF; 
    246246  canvas->fg.on = TRUE; 
    247   canvas->bg.r = 0.; 
    248   canvas->bg.g = 0.; 
    249   canvas->bg.b = 0.; 
    250   canvas->bg.a = 1.; 
     247  canvas->bg.r = 0x00; 
     248  canvas->bg.g = 0x00; 
     249  canvas->bg.b = 0x00; 
     250  canvas->bg.a = 0xFF; 
    251251  canvas->bg.on = TRUE; 
    252252  canvas->mode = s_center; 
     
    299299shoes_canvas_stroke(int argc, VALUE *argv, VALUE self) 
    300300{ 
    301   VALUE _r, _g, _b, _a; 
    302   SETUP(); 
    303  
    304   rb_scan_args(argc, argv, "31", &_r, &_g, &_b, &_a); 
    305   canvas->fg.on = TRUE; 
    306   canvas->fg.r = NUM2DBL(_r); 
    307   canvas->fg.g = NUM2DBL(_g); 
    308   canvas->fg.b = NUM2DBL(_b); 
    309   canvas->fg.a = 1.0; 
    310   if (!NIL_P(_a)) canvas->fg.a = NUM2DBL(_a); 
     301  shoes_color *color; 
     302  VALUE _r, _g, _b, _a, _color; 
     303  SETUP(); 
     304 
     305  argc = rb_scan_args(argc, argv, "13", &_r, &_g, &_b, &_a); 
     306  if (argc == 1 && rb_obj_is_kind_of(_r, cColor)) 
     307    _color = _r; 
     308  else if (argc == 1 && rb_obj_is_kind_of(_r, rb_cString)) 
     309    _color = shoes_color_parse(cColor, _r); 
     310  else if (argc == 1 || argc == 2) 
     311    _color = shoes_color_gray(argc, argv, cColor); 
     312  else 
     313    _color = shoes_color_rgb(argc, argv, cColor); 
     314 
     315  Data_Get_Struct(_color, shoes_color, color); 
     316  canvas->fg = *color; 
    311317 
    312318  return self; 
     
    333339shoes_canvas_fill(int argc, VALUE *argv, VALUE self) 
    334340{ 
    335   VALUE _r, _g, _b, _a; 
    336   SETUP(); 
    337  
    338   rb_scan_args(argc, argv, "31", &_r, &_g, &_b, &_a); 
    339   canvas->bg.on = TRUE; 
    340   canvas->bg.r = NUM2DBL(_r); 
    341   canvas->bg.g = NUM2DBL(_g); 
    342   canvas->bg.b = NUM2DBL(_b); 
    343   canvas->bg.a = 1.0; 
    344   if (!NIL_P(_a)) canvas->bg.a = NUM2DBL(_a); 
     341  shoes_color *color; 
     342  VALUE _r, _g, _b, _a, _color; 
     343  SETUP(); 
     344 
     345  argc = rb_scan_args(argc, argv, "13", &_r, &_g, &_b, &_a); 
     346  if (argc == 1 && rb_obj_is_kind_of(_r, cColor)) 
     347    _color = _r; 
     348  else if (argc == 1 && rb_obj_is_kind_of(_r, rb_cString)) 
     349    _color = shoes_color_parse(cColor, _r); 
     350  else if (argc == 1 || argc == 2) 
     351    _color = shoes_color_gray(argc, argv, cColor); 
     352  else 
     353    _color = shoes_color_rgb(argc, argv, cColor); 
     354 
     355  Data_Get_Struct(_color, shoes_color, color); 
     356  canvas->bg = *color; 
    345357 
    346358  return self; 
  • trunk/shoes/canvas.h

    r119 r120  
    2727// 
    2828typedef struct { 
    29   double r, g, b, a; 
    30   unsigned char on; 
     29  unsigned char r, g, b, a, on; 
    3130} shoes_color; 
    3231 
     
    181180VALUE shoes_canvas_nofill(VALUE); 
    182181VALUE shoes_canvas_fill(int, VALUE *, VALUE); 
     182VALUE shoes_canvas_rgb(int, VALUE *, VALUE); 
     183VALUE shoes_canvas_gray(int, VALUE *, VALUE); 
    183184VALUE shoes_canvas_rect(int, VALUE *, VALUE); 
    184185VALUE shoes_canvas_oval(VALUE, VALUE, VALUE, VALUE, VALUE); 
     
    278279void shoes_anim_call(VALUE); 
    279280 
    280 VALUE shoes_color_new(double, double, double, double); 
     281VALUE shoes_color_new(int, int, int, int); 
    281282VALUE shoes_color_alloc(VALUE); 
    282283VALUE shoes_color_rgb(int, VALUE *, VALUE); 
    283284VALUE shoes_color_gray(int, VALUE *, VALUE); 
     285cairo_pattern_t *shoes_color_pattern(VALUE); 
    284286VALUE shoes_color_parse(VALUE, VALUE); 
    285287VALUE shoes_color_to_s(VALUE); 
  • trunk/shoes/ruby.c

    r119 r120  
    497497  if (self_t->bg.on) 
    498498  { 
    499     cairo_set_source_rgba(canvas->cr, self_t->bg.r, self_t->bg.g, self_t->bg.b, self_t->bg.a); 
     499    cairo_set_source_rgba(canvas->cr, self_t->bg.r / 255., self_t->bg.g / 255., self_t->bg.b / 255., self_t->bg.a / 255.); 
    500500    cairo_fill_preserve(canvas->cr); 
    501501  } 
    502502  if (self_t->fg.on) 
    503503  { 
    504     cairo_set_source_rgba(canvas->cr, self_t->fg.r, self_t->fg.g, self_t->fg.b, self_t->fg.a); 
     504    cairo_set_source_rgba(canvas->cr, self_t->fg.r / 255., self_t->fg.g / 255., self_t->fg.b / 255., self_t->fg.a / 255.); 
    505505    cairo_stroke_preserve(canvas->cr); 
    506506  } 
     
    608608  shoes_pattern *pattern; 
    609609  VALUE obj = shoes_pattern_alloc(klass); 
    610   VALUE rgb = rb_funcall(source, s_match, 1, reRGB_SOURCE); 
     610  VALUE rgb; 
    611611  Data_Get_Struct(obj, shoes_pattern, pattern); 
    612612  pattern->source = source; 
    613   if (!NIL_P(rgb)) 
    614   { 
    615     int r = NUM2INT(rb_Integer(rb_reg_nth_match(1, rgb))); 
    616     int g = NUM2INT(rb_Integer(rb_reg_nth_match(2, rgb))); 
    617     int b = NUM2INT(rb_Integer(rb_reg_nth_match(3, rgb))); 
    618     pattern->pattern = cairo_pattern_create_rgb(r / 255., g / 255., b / 255.); 
     613 
     614  if (!rb_obj_is_kind_of(source, cColor)) 
     615  { 
     616    rgb = shoes_color_parse(cColor, source); 
     617    if (!NIL_P(rgb)) source = rgb; 
     618  } 
     619 
     620  if (rb_obj_is_kind_of(source, cColor)) 
     621  { 
     622    pattern->pattern = shoes_color_pattern(source); 
    619623    pattern->width = pattern->height = 1.; 
    620624  } 
     
    627631  } 
    628632  cairo_pattern_set_extend(pattern->pattern, CAIRO_EXTEND_REPEAT); 
     633 
    629634  pattern->attr = attr; 
    630635  pattern->parent = parent; 
     
    698703 
    699704VALUE 
    700 shoes_color_new(double r, double g, double b, double a) 
     705shoes_color_new(int r, int g, int b, int a) 
    701706{ 
    702707  shoes_color *color; 
     
    715720  shoes_color *color; 
    716721  VALUE obj = Data_Make_Struct(klass, shoes_color, shoes_color_mark, shoes_color_free, color); 
    717   color->r = 0.0; 
    718   color->g = 0.0; 
    719   color->b = 0.0; 
    720   color->a = 1.0; 
     722  color->r = 0x00; 
     723  color->g = 0x00; 
     724  color->b = 0x00; 
     725  color->a = 0xFF; 
    721726  color->on = TRUE; 
    722727  return obj; 
     
    726731shoes_color_rgb(int argc, VALUE *argv, VALUE self) 
    727732{ 
    728   double a; 
     733  int a; 
    729734  VALUE _r, _g, _b, _a; 
    730735  rb_scan_args(argc, argv, "31", &_r, &_g, &_b, &_a); 
    731736 
    732   a = 1.0; 
    733   if (!NIL_P(a)) a = NUM2DBL(_a); 
    734   return shoes_color_new(NUM2DBL(_r), NUM2DBL(_g), NUM2DBL(_b), a); 
     737  a = 0xFF; 
     738  if (!NIL_P(_a)) a = NUM2RGBINT(_a); 
     739  return shoes_color_new(NUM2RGBINT(_r), NUM2RGBINT(_g), NUM2RGBINT(_b), a); 
    735740} 
    736741 
     
    740745  shoes_color *color; 
    741746  VALUE _g, _a; 
    742   double g, a; 
     747  int g, a; 
    743748  rb_scan_args(argc, argv, "11", &_g, &_a); 
    744749 
    745   a = 1.0; 
    746   g = NUM2DBL(_g); 
    747   if (!NIL_P(a)) a = NUM2DBL(_a); 
     750  a = 0xFF; 
     751  g = NUM2RGBINT(_g); 
     752  if (!NIL_P(_a)) a = NUM2RGBINT(_a); 
    748753  return shoes_color_new(g, g, g, a); 
     754} 
     755 
     756cairo_pattern_t * 
     757shoes_color_pattern(VALUE obj) 
     758{ 
     759  shoes_color *color; 
     760  Data_Get_Struct(obj, shoes_color, color); 
     761  if (color->a == 255) 
     762    return cairo_pattern_create_rgb(color->r / 255., color->g / 255., color->b / 255.); 
     763  else 
     764    return cairo_pattern_create_rgba(color->r / 255., color->g / 255., color->b / 255., color->a / 255.); 
    749765} 
    750766 
     
    760776  if (!NIL_P(reg)) 
    761777  { 
    762     color->r = NUM2DBL(rb_str2inum(rb_reg_nth_match(1, reg), 16) * 17) / 255.; 
    763     color->g = NUM2DBL(rb_str2inum(rb_reg_nth_match(2, reg), 16) * 17) / 255.; 
    764     color->b = NUM2DBL(rb_str2inum(rb_reg_nth_match(3, reg), 16) * 17) / 255.; 
     778    color->r = NUM2INT(rb_str2inum(rb_reg_nth_match(1, reg), 16)) * 17; 
     779    color->g = NUM2INT(rb_str2inum(rb_reg_nth_match(2, reg), 16)) * 17; 
     780    color->b = NUM2INT(rb_str2inum(rb_reg_nth_match(3, reg), 16)) * 17; 
    765781    return obj; 
    766782  } 
     
    769785  if (!NIL_P(reg)) 
    770786  { 
    771     color->r = NUM2DBL(rb_str2inum(rb_reg_nth_match(1, reg), 16)) / 255.; 
    772     color->g = NUM2DBL(rb_str2inum(rb_reg_nth_match(2, reg), 16)) / 255.; 
    773     color->b = NUM2DBL(rb_str2inum(rb_reg_nth_match(3, reg), 16)) / 255.; 
     787    color->r = NUM2INT(rb_str2inum(rb_reg_nth_match(1, reg), 16)); 
     788    color->g = NUM2INT(rb_str2inum(rb_reg_nth_match(2, reg), 16)); 
     789    color->b = NUM2INT(rb_str2inum(rb_reg_nth_match(3, reg), 16)); 
    774790    return obj; 
    775791  } 
     
    778794  if (!NIL_P(reg)) 
    779795  { 
    780     color->r = NUM2DBL(rb_Integer(rb_reg_nth_match(1, reg))) / 255.; 
    781     color->g = NUM2DBL(rb_Integer(rb_reg_nth_match(2, reg))) / 255.; 
    782     color->b = NUM2DBL(rb_Integer(rb_reg_nth_match(3, reg))) / 255.; 
     796    color->r = NUM2INT(rb_Integer(rb_reg_nth_match(1, reg))); 
     797    color->g = NUM2INT(rb_Integer(rb_reg_nth_match(2, reg))); 
     798    color->b = NUM2INT(rb_Integer(rb_reg_nth_match(3, reg))); 
    783799    return obj; 
    784800  } 
     
    787803  if (!NIL_P(reg)) 
    788804  { 
    789     color->r = NUM2DBL(rb_Integer(rb_reg_nth_match(1, reg))) / 255.; 
    790     color->g = NUM2DBL(rb_Integer(rb_reg_nth_match(2, reg))) / 255.; 
    791     color->b = NUM2DBL(rb_Integer(rb_reg_nth_match(3, reg))) / 255.; 
    792     color->a = NUM2DBL(rb_Integer(rb_reg_nth_match(4, reg))) / 255.; 
     805    color->r = NUM2INT(rb_Integer(rb_reg_nth_match(1, reg))); 
     806    color->g = NUM2INT(rb_Integer(rb_reg_nth_match(2, reg))); 
     807    color->b = NUM2INT(rb_Integer(rb_reg_nth_match(3, reg))); 
     808    color->a = NUM2INT(rb_Integer(rb_reg_nth_match(4, reg))); 
    793809    return obj; 
    794810  } 
     
    797813  if (!NIL_P(reg)) 
    798814  { 
    799     color->r = color->g = color->b = NUM2DBL(rb_Integer(rb_reg_nth_match(1, reg))) / 255.; 
     815    color->r = color->g = color->b = NUM2INT(rb_Integer(rb_reg_nth_match(1, reg))); 
    800816    return obj; 
    801817  } 
     
    804820  if (!NIL_P(reg)) 
    805821  { 
    806     color->r = color->g = color->b = NUM2DBL(rb_Integer(rb_reg_nth_match(1, reg))) / 255.; 
    807     color->a = NUM2DBL(rb_Integer(rb_reg_nth_match(2, reg))) / 255.; 
     822    color->r = color->g = color->b = NUM2INT(rb_Integer(rb_reg_nth_match(1, reg))); 
     823    color->a = NUM2INT(rb_Integer(rb_reg_nth_match(2, reg))); 
    808824    return obj; 
    809825  } 
     
    817833  shoes_color *color; 
    818834  Data_Get_Struct(self, shoes_color, color); 
    819   VALUE ary = rb_ary_new3(4, INT2NUM(color->r * 255), INT2NUM(color->g * 255), INT2NUM(color->b * 255), INT2NUM(color->a * 255)); 
     835  VALUE ary = rb_ary_new3(4, INT2NUM(color->r), INT2NUM(color->g), INT2NUM(color->b), INT2NUM(color->a)); 
    820836  if (color->a == 1.0) 
    821837    return rb_funcall(rb_str_new2("rgb(%d, %d, %d)"), s_perc, 1, ary); 
     
    12321248  if (canvas->fg.on) 
    12331249  { 
    1234     cairo_set_source_rgba(canvas->cr, canvas->fg.r, canvas->fg.g, canvas->fg.b, canvas->fg.a); 
     1250    cairo_set_source_rgba(canvas->cr, canvas->fg.r / 255., canvas->fg.g / 255., canvas->fg.b / 255., canvas->fg.a / 255.); 
    12351251  // cairo_stroke(canvas->cr); 
    12361252  } 
     
    19852001  rb_define_method(cCanvas, "nofill", CASTHOOK(shoes_canvas_nofill), 0); 
    19862002  rb_define_method(cCanvas, "fill", CASTHOOK(shoes_canvas_fill), -1); 
     2003  rb_define_method(cCanvas, "rgb", CASTHOOK(shoes_color_rgb), -1); 
     2004  rb_define_method(cCanvas, "gray", CASTHOOK(shoes_color_gray), -1); 
    19872005  rb_define_method(cCanvas, "rect", CASTHOOK(shoes_canvas_rect), -1); 
    19882006  rb_define_method(cCanvas, "oval", CASTHOOK(shoes_canvas_oval), 4); 
  • trunk/shoes/ruby.h

    r119 r120  
    4949  "end;" 
    5050 
     51#define NUM2RGBINT(x) (rb_obj_is_kind_of(x, rb_cFloat) ? NUM2DBL(x) * 255 : NUM2INT(x)) 
     52 
    5153// 
    5254// Common funcs for dealing with attribute hashes