require 'gosu' require 'texplay' class Canvas < Gosu::Window def initialize(width = 500, height = 500) super(width, height, false) self.caption = "---=== CANVAS ===---" @canvas = TexPlay::create_image(self, width, height, :color => :white) @supported_colors = ["black", "blue", "brown", "cyan", "green", "grey", "orange", "purple", "red", "turquoise", "tyrian", "white", "yellow"] @extra_colors = { "darkblue" => [0, 0, 0.545], "lightblue" => [0.678, 0.847, 0.902], "darkgreen" => [0, 0.392, 0], "tomato" => [1, 0.388, 0.278], "darkred" => [0.545, 0, 0], "pink" => [1, 0.753, 0.796], "deeppink" => [1, 0.078, 0.576] } end def draw @canvas.draw(0, 0, 0) end def paint_background(color) color = check_color(color) @canvas.paint { fill 0, 0, :color => color } end def draw_circle(x, y, r, color = "black", fill = false) color = check_color(color) @canvas.paint { circle x, y, r, :color => color, :fill => fill } end def draw_box(x, y, width, height, color = "black", fill = false) color = check_color(color) @canvas.paint { rect x, y, x + width, y + height, :color => color, :fill => fill } end def draw_triangle(x1, y1, x2, y2, x3, y3, color = "black", thickness = 1) color = check_color(color) @canvas.paint { line x1, y1, x2, y2, :color => color, :thickness => thickness line x2, y2, x3, y3, :color => color, :thickness => thickness line x3, y3, x1, y1, :color => color, :thickness => thickness } end def draw_line(x1, y1, x2, y2, color = "black", thickness = 1) color = check_color(color) @canvas.paint { line x1, y1, x2, y2, :color => color, :thickness => thickness } end def draw_polygon(x, y, r, sides, color = "black", thickness = 1) color = check_color(color) @canvas.paint { ngon x, y, r, sides, :color => color, :thickness => thickness } end def fill(x, y, color = "black") color = check_color(color) @canvas.paint { fill x, y, :color => color } end def check_color(color) if @supported_colors.include?(color) color.to_sym elsif @extra_colors.has_key?(color) @extra_colors[color] else :black end end end