From c477432239f61c7d0f47f2c0fc3abfc2472addaf Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Thu, 17 Jul 2014 13:11:26 -0400 Subject: [PATCH] Color handling handed off to rcairo --- lib/squib/api/background.rb | 3 ++- lib/squib/api/text.rb | 3 ++- lib/squib/deck.rb | 6 ++++++ lib/squib/graphics/background.rb | 2 +- lib/squib/graphics/text.rb | 6 +++--- samples/basic.rb | 6 ++---- samples/excel.rb | 2 +- spec/deck_spec.rb | 10 +++++----- 8 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/squib/api/background.rb b/lib/squib/api/background.rb index 806fd9c..5b50abe 100644 --- a/lib/squib/api/background.rb +++ b/lib/squib/api/background.rb @@ -2,8 +2,9 @@ module Squib class Deck #module API - def background(range: :all, color: '#000000') + def background(range: :all, color: :black) range = rangeify(range) + color = colorify(color) range.each { |i| @cards[i].background(color) } end diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index 3c74a68..50bf819 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -17,8 +17,9 @@ module Squib range = rangeify(range) str = [str] * @cards.size unless str.respond_to? :each font = fontify(font) + color = colorify(options[:color]) range.each do |i| - cards[i].text(str[i], font, x, y, options) + cards[i].text(str[i], font, x, y, color, options) end end diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 67ae36c..3055132 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -38,6 +38,12 @@ module Squib file end + def colorify(color) + color ||= :black + color = Cairo::Color.parse(color) + color + end + ################## ### PUBLIC API ### ################## diff --git a/lib/squib/graphics/background.rb b/lib/squib/graphics/background.rb index a2458f3..c635b67 100644 --- a/lib/squib/graphics/background.rb +++ b/lib/squib/graphics/background.rb @@ -3,7 +3,7 @@ module Squib def background(color) cc = cairo_context - cc.set_source_rgb(*color) + cc.set_source_color(color) cc.paint end diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 2f38a82..48091cc 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -3,14 +3,14 @@ require 'pango' module Squib class Card - def text(str, font, x, y, options) + def text(str, font, x, y, color, options) cc = cairo_context - cc.set_source_color(:black) #black + cc.set_source_color(color) cc.move_to(x,y) layout = cc.create_pango_layout layout.text = str.to_s w = options[:width] ; h = options[:height] - w ||= (@width - 2*x) ; h ||= (@height - 2*y) # default centers to x,y + w ||= (@width - 2*x); h ||= (@height - 2*y) # default centers to x,y w *= Pango::SCALE ; h *= Pango::SCALE layout.width=w ; layout.height=h layout.wrap = Pango::WRAP_WORD diff --git a/samples/basic.rb b/samples/basic.rb index 389fcb5..1269698 100644 --- a/samples/basic.rb +++ b/samples/basic.rb @@ -3,18 +3,16 @@ require 'squib' data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} -longtext = "Hello, World! What do you know about tweetle beetles? well... \nWhen tweetle beetles fight, it's called a tweetle beetle battle. And when they battle in a puddle, it's a tweetle beetle puddle battle. AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle. AND... When beetles battle beetles in a puddle paddle battle and the beetle battle puddle is a puddle in a bottle... ..they call this a tweetle beetle bottle puddle paddle battle muddle. AND... When beetles fight these battles in a bottle with their paddles and the bottle's on a poodle and the poodle's eating noodles... ...they call this a muddle puddle tweetle poodle beetle noodle bottle paddle battle." Squib::Deck.new(width: 825, height: 1125, cards: 3) do - background color: [1.0,1.0,1.0] + background color: :white rect x: 15, y: 15, width: 795, height: 1095, x_radius: 50, y_radius: 50 rect x: 30, y: 30, width: 128, height: 128, x_radius: 25, y_radius: 25 text str: data['name'], x: 250, y: 55, font: 'Arial 54' text str: data['level'], x: 65, y: 40, font: 'Arial 72' - text str: longtext, x: 100, y: 600, font: 'Arial 16' - png file: 'shiny-purse.png', x: 665, y: 30 + png range: [0,2], file: 'shiny-purse.png', x: 665, y: 30 svg range: 1..2, file: 'spanner.svg', x: 665, y: 165 save format: :png diff --git a/samples/excel.rb b/samples/excel.rb index faeb2cb..f74d3a8 100644 --- a/samples/excel.rb +++ b/samples/excel.rb @@ -2,7 +2,7 @@ require 'squib' Squib::Deck.new(width: 825, height: 1125, cards: 3) do - background color: [1.0,1.0,1.0] + background color: :white data = xlsx file: 'sample.xlsx', sheet: 0 diff --git a/spec/deck_spec.rb b/spec/deck_spec.rb index 146c9f3..f624986 100644 --- a/spec/deck_spec.rb +++ b/spec/deck_spec.rb @@ -13,8 +13,8 @@ describe Squib::Deck do it "calls text on all cards by default" do card1 = instance_double(Squib::Card) card2 = instance_double(Squib::Card) - expect(card1).to receive(:text).with('blah','Arial 36',0,0,{}).once - expect(card2).to receive(:text).with('blah','Arial 36',0,0,{}).once + expect(card1).to receive(:text).once + expect(card2).to receive(:text).once Squib::Deck.new do @cards = [card1, card2] text str: 'blah' @@ -24,7 +24,7 @@ describe Squib::Deck do it "calls text on some cards with an integer" do card1 = instance_double(Squib::Card) card2 = instance_double(Squib::Card) - expect(card2).to receive(:text).with('blah','Arial 36',0,0,{}).once + expect(card2).to receive(:text).once Squib::Deck.new do @cards = [card1, card2] text range: 1, str: 'blah' @@ -35,8 +35,8 @@ describe Squib::Deck do card1 = instance_double(Squib::Card) card2 = instance_double(Squib::Card) card3 = instance_double(Squib::Card) - expect(card1).to receive(:text).with('blah','Arial 36',0,0,{}).once - expect(card2).to receive(:text).with('blah','Arial 36',0,0,{}).once + expect(card1).to receive(:text).once + expect(card2).to receive(:text).once Squib::Deck.new do @cards = [card1, card2, card3] text range: 0..1, str: 'blah'