lib/shogi_koma/painter.rb in shogi_koma-0.0.2 vs lib/shogi_koma/painter.rb in shogi_koma-0.0.3
- old
+ new
@@ -1,16 +1,47 @@
require "cairo"
module ShogiKoma
class Painter
attr_accessor :width, :height, :font
+ attr_reader :body_color, :text_color
def initialize
@width = 200
@height = 200
@font = "IPAMincho"
+ set_body_rgb(1, 0.8, 0.2)
+ set_frame_color(:black)
+ set_text_color(:black)
end
+ def set_body_color(color)
+ @body_color = Cairo::Color.parse(color)
+ end
+
+ def set_body_rgba(r, g, b, a=1.0)
+ @body_color = Cairo::Color.parse([:rgba, r, g, b, a])
+ end
+ alias :set_body_rgb :set_body_rgba
+
+ def set_frame_color(color)
+ @frame_color = Cairo::Color.parse(color)
+ end
+
+ def set_frame_rgba(r, g, b, a=1.0)
+ @frame_color = Cairo::Color.parse([:rgba, r, g, b, a])
+ end
+ alias :set_frame_rgb :set_frame_rgba
+
+ def set_text_color(color)
+ @text_color = Cairo::Color.parse(color)
+ end
+
+ def set_text_rgba(r, g, b, a=1.0)
+ @text_color = Cairo::Color.parse([:rgba, r, g, b, a])
+ end
+ alias :set_text_rgb :set_text_rgba
+
def write_to_png(text, output_path)
Cairo::ImageSurface.new(:argb32, @width, @height) do |surface|
Cairo::Context.new(surface) do |context|
context.scale(@width, @height)
draw(context, text)
@@ -20,24 +51,24 @@
end
def draw(context, text)
draw_body(context)
text = divide(text)
- send("draw_text#{text.length}", context, text)
+ __send__("draw_text#{text.length}", context, text)
end
def draw_body(context)
context.set_line_width(0.01)
context.move_to(0.2, 0.2)
context.line_to(0.5, 0.1)
context.line_to(0.8, 0.2)
context.line_to(0.9, 0.9)
context.line_to(0.1, 0.9)
context.close_path
- context.set_source_rgb(1, 0.8, 0.2)
+ context.set_source_color(@body_color)
context.fill_preserve
- context.set_source_color(:black)
+ context.set_source_color(@frame_color)
context.stroke
end
def divide(text)
case text.length
@@ -57,17 +88,19 @@
end
end
end
def draw_text1(context, text)
+ context.set_source_color(@text_color)
text = text[0] if text.is_a?(Array)
context.select_font_face(@font)
context.font_size = 0.6
context.move_to(0.2, 0.75)
context.show_text(text)
end
def draw_text2(context, text)
+ context.set_source_color(@text_color)
context.select_font_face(@font)
context.font_size = 0.4
context.move_to(0.3, 0.49)
context.show_text(text[0])
context.move_to(0.3, 0.85)