ruby-****@sourc*****
ruby-****@sourc*****
2013年 4月 5日 (金) 02:35:43 JST
------------------------- REMOTE_ADDR = 70.49.48.128 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-dancr-rbcatut-dwc ------------------------- @@ -263,6 +263,115 @@ puts "DEBUG: cr.target.class=#{cr.target.class}" #=> Cairo::ImageSurface + :Extension Axis Of Cairo Gradients + (12.3.2.1.2.A1){{br}} + + The API documentation for cairo pattern constructors tell us that a new linear gradient cairo pattern will be created along the line defined by (x0, y0) and (x1, y1), or that a new radial gradient cairo_pattern_t between the two circles defined by (cx0, cy0, radius0) and (cx1, cy1, radius1). What do these two statements actually mean one can only be sure, after some practical experimenting is done. This subsection is designed to speed up your understanding. Here, we will base our investigation of this issue, by looking at the ((<Cairo: :LinearPattern.new(x0,y0,x1,y1)|URL:http://cairo.rubyforge.org/doc/en/cairo-linear-pattern.html#label-3>)) constructor (for which unfortunately the only reasonable documentation can currently be found on ((<cairo_pattern_create_linear ()|URL:http://www.cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-linear>)).) After that you should have no trouble understanding the documentation for ((<cairo_pattern_create_rad ial ()|URL:http://www.cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-radial>)) which too, applies to its Ruby counterpart found and also, hopefully soon, documented at ((<Cairo: :RadialPattern.new(cx1,cy1,r1,cx2,cy2,r2)|URL:http://cairo.rubyforge.org/doc/en/cairo-radial-pattern.html#label-3>)). + + + As you can see, in the above example, the following "Cairo: :LinearPattern.new(x1,y1,x2,y2)" constructor is used. Obviously, the four parameters to the constructor define that line (or as I prefer to call it((*the extensin axis*)) of cairo gradients) along which the API documentation, tells us the 'linear gradient cairo pattern' will be created. This extension axis is the black line on the image on the right side next to the code segment with the constructor: + + {{image_right("1203-p10-82-LinearPattern-axis-orig-s2.png")}} + + linpat = Cairo::LinearPattern.new(0.25, 0.35, 0.75, 0.65) + + On the image on the right you can see the line defined by end points (x1=0.25, y1=0.35) and (x2=0.75, y2=0.65). If you change the constructor parameters to define the extension axis from the top left to the bottom right corner, i.e. (x1=0, y1=0) and (x2=1, y2=1), you'd have the following change: + + + {{image_right("1203-p10-82-LinearPattern-axis-x0y1-s2.png")}} + + linpat = Cairo::LinearPattern.new(0, 0, 1, 1) + + + However, neither of these two images reveal very much about the extensions of cairo gradients, particularly, if you are interested to understand what the cairo extend mode constants defined in ((<Cairo: :Extend module|URL:http://cairo.rubyforge.org/doc/en/cairo-extend.html#label-0>)), and explained in ((<enum cairo_extend_t|URL:http://www.cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-extend-t>)), do. + + To help you understand this, I wrote the following cairo gradients testing program: + + {{image_right("1203-p10-83-diff-CrExtends-4-diff-lin-patts-axis-s2.png")}} + +# FILE: 1203-p10-81-01-50-03-ssradnt-multi-LINEARs-trOK-GtkCr-dpl01.rb + + #!/usr/bin/env ruby + $: << '~/work/HikiLib' + require 'hiki2-gtk-w-cairo.rb' + include HikiGtk + + # -- Testing different linear gradients for the different axis. Uncomment the + # line with desired LinearPattern axis end points x1,y1 and x2,y2: + #$lpttx1 = 0.25; $lptty1 = 0.35; $lpttx2 = 0.75; $lptty2 = 0.65 + #$lpttx1 = 0.20; $lptty1 = 0.20; $lpttx2 = 0.40; $lptty2 = 0.60 + $lpttx1 = 0.10; $lptty1 = 0.10; $lpttx2 = 0.40; $lptty2 = 0.40 + + class DrwAreaToImg < CairoWindow + def draw(cr, drawing_area) + #width, height = drawing_area.window.size + + # Your code goes between the two dashed lines: + # -- your code - start ----------------------------------------- -s- + cr.scale(120, 120) # Examples are in 1x1 coordinate space + xpos = ypos = gap = 0.03 + w = 1 + extend_arr = %w[NONE PAD REFLECT REPEAT] + + radpat = Cairo::RadialPattern.new(0.25, 0.25, 0.1, 0.5, 0.5, 0.5) + radpat.add_color_stop_rgb(0, 1.0, 0.8, 0.8) + radpat.add_color_stop_rgb(1, 0.9, 0.0, 0.0) + # Cairo::Extend::{NONE, PAD, REFLECT, REPEAT}; need: {{ require 'cairo' }} + # radpat.set_extend(Cairo::Extend::NONE) # Default + linpat = Cairo::LinearPattern.new($lpttx1, $lptty1, $lpttx2, $lptty2) + linpat.add_color_stop_rgba(0.00, 1, 1, 1, 0) + linpat.add_color_stop_rgba(0.25, 0, 1, 0, 0.5) + linpat.add_color_stop_rgba(0.50, 1, 1, 1, 0) + linpat.add_color_stop_rgba(0.75, 0, 0, 1, 0.5) + linpat.add_color_stop_rgba(1.00, 1, 1, 1, 0) + + 0.upto(extend_arr.size - 1) do |cnt| + cr.save # Ignore save and restore in this example. + cr.translate(xpos, ypos) # Ignore until you read: 12.3.4 (Working with Transforms). + xpos += (w + gap) + + # Cairo::Extend::{NONE, PAD, REFLECT, REPEAT}; need: {{ require 'cairo' }} + linpat.set_extend(eval("Cairo::Extend::#{extend_arr[cnt]}")) + + 1.upto(9) do |i| + 1.upto(9) do |j| + cr.rectangle(i/10.0 - 0.04, j/10.0 - 0.04, 0.08, 0.08) + end + end + + cr.set_source(radpat) + cr.fill + + # --- show LinearPattern axis ----- -s- + cr.line_width = 0.01 + cr.set_source_rgb(0, 0, 0) + cr.move_to( $lpttx1, $lptty1 ) + cr.rel_line_to( $lpttx2, $lptty2 ) + cr.stroke + # --- show LinearPattern axis ----- -e- + + cr.rectangle(0, 0, 1, 1) + cr.set_source(linpat) + cr.fill + + cr.restore # Ignore {{ save }} and {{ restore }} in this example. + end + # -- Your cairo code ends here ---------------- -e- + end + end + + title = "Diff. gradients axis: x1,y1=" + + "(%.2f,%.2f), x2,y2=(%.2f,%.2f)" % [$lpttx1, $lptty1, $lpttx2, $lptty2] + w=DrwAreaToImg.new(title) + w.set_size_request(497, 125) + w.show_all + Gtk.main + + + + + + {{br}} :Image Sources (12.3.2.1.3){{br}}