[cairo-commit] rcairo/src/lib/cairo color.rb, 1.3, 1.4 pattern.rb, 1.1, 1.2

Kouhei Sutou commit at pdx.freedesktop.org
Mon Apr 16 00:31:53 PDT 2007


Committed by: kou

Update of /cvs/cairo/rcairo/src/lib/cairo
In directory kemper:/tmp/cvs-serv31172/src/lib/cairo

Modified Files:
	color.rb pattern.rb 
Log Message:
* src/lib/cairo/color.rb, src/lib/cairo/pattern.rb,
  src/lib/cairo/context/color.rb, src/rb_cairo_pattern.c: improved
  Cairo::Color interface.

* sample/: used Cairo::Color.


Index: color.rb
===================================================================
RCS file: /cvs/cairo/rcairo/src/lib/cairo/color.rb,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- color.rb	16 Apr 2007 05:55:41 -0000	1.3
+++ color.rb	16 Apr 2007 07:31:43 -0000	1.4
@@ -1,5 +1,49 @@
 module Cairo
   module Color
+    module_function
+    def parse(value)
+      return value.dup if value.is_a?(Base)
+      case value
+      when Array
+        case value.first
+        when :cmyk, :cmyka
+          CMYK.new(*value[1..-1])
+        else
+          type, *value = value if [:rgb, :rgba].include?(value.first)
+          RGB.new(*value)
+        end
+      when /\A#/ #
+        parse_hex_color(value)
+      when String, Symbol
+        name = value.to_s.gsub(/[\s\-]+/, '_').upcase
+        begin
+          const_get(name).dup
+        rescue NameError
+          raise ArgumentError, "unknown color name: #{value}"
+        end
+      else
+        # can't parse. should raise?
+        value
+      end
+    end
+
+    HEX_RE = "(?i:[a-f\\d])"
+    def parse_hex_color(value)
+      case value
+      when /\A#((?:#{HEX_RE}){3,4})\z/ #
+        RGB.new(*$1.scan(/./).collect {|value| value.hex / 15.0})
+      when /\A#((?:#{HEX_RE}{2,2}){3,4})\z/ #
+        RGB.new(*$1.scan(/.{2,2}/).collect {|value| value.hex / 255.0})
+      when /\A#((?:#{HEX_RE}{4,4}){3,4})\z/ #
+        RGB.new(*$1.scan(/.{4,4}/).collect {|value| value.hex / 65535.0})
+      else
+        message = "invalid hex color format: #{value} should be "
+        message << "#RGB, #RGBA, #RRGGBB, #RRGGBBAA, #RRRRGGGGBBBB "
+        message << "or #RRRRGGGGBBBBAAAA"
+        raise ArgumentError, message
+      end
+    end
+
     class Base
       attr_accessor :alpha
 
@@ -45,6 +89,10 @@
       end
       alias_method :to_ary, :to_a
 
+      def to_s
+        "#%02X%02X%02X%02X" % to_a.collect {|v| (v * 255).ceil}
+      end
+
       def to_rgb
         clone
       end

Index: pattern.rb
===================================================================
RCS file: /cvs/cairo/rcairo/src/lib/cairo/pattern.rb,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- pattern.rb	16 Apr 2007 03:12:49 -0000	1.1
+++ pattern.rb	16 Apr 2007 07:31:43 -0000	1.2
@@ -10,12 +10,12 @@
   end
 
   class GradientPattern
-    def add_color_stop(*args)
-      if args.size == 1 and args.first.is_a?(Color)
-        add_color_stop_rgba(*args.first.to_rgb.to_a)
-      else
-        add_color_stop_rgba(*args)
+    def add_color_stop(offset, *args)
+      if args.size == 1
+        color = Color.parse(args.first)
+        args = color.to_rgb.to_a
       end
+      add_color_stop_rgba(offset, *args)
     end
 
     if method_defined?(:get_color_stop_rgba)



More information about the cairo-commit mailing list