Class: DynamicImageSources::ColorSource
- Inherits:
-
SourceFactory
- Object
- SourceFactory
- DynamicImageSources::ColorSource
- Defined in:
- lib/sources/color_source.rb
Overview
Source providing solid color to use as source.
Instance Attribute Summary collapse
-
#alpha ⇒ Object
readonly
Gets color component.
-
#blue ⇒ Object
readonly
Gets color component.
-
#green ⇒ Object
readonly
Gets color component.
-
#red ⇒ Object
readonly
Gets color component.
Class Method Summary collapse
-
.named_colors ⇒ Object
Gives
Array
of all known named colors. -
.parse(source) ⇒ Object
Returns source object or nil if it can’t parse it.
Instance Method Summary collapse
-
#initialize(color, alpha) ⇒ ColorSource
constructor
Creates source object from
Cairo::Color::RGB
object and alpha as Float value. -
#set_source(context, x, y, w, h) ⇒ Object
Sets color as source to given context.
Constructor Details
#initialize(color, alpha) ⇒ ColorSource
Creates source object from Cairo::Color::RGB
object and alpha as Float value.
7 8 9 10 11 12 13 |
# File 'lib/sources/color_source.rb', line 7 def initialize(color, alpha) alpha = nil unless alpha.class == Float @red = color.red @green = color.green @blue = color.blue @alpha = alpha || 1 end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Gets color component
16 17 18 |
# File 'lib/sources/color_source.rb', line 16 def alpha @alpha end |
#blue ⇒ Object (readonly)
Gets color component
16 17 18 |
# File 'lib/sources/color_source.rb', line 16 def blue @blue end |
#green ⇒ Object (readonly)
Gets color component
16 17 18 |
# File 'lib/sources/color_source.rb', line 16 def green @green end |
#red ⇒ Object (readonly)
Gets color component
16 17 18 |
# File 'lib/sources/color_source.rb', line 16 def red @red end |
Class Method Details
.named_colors ⇒ Object
Gives Array
of all known named colors. See cairo.rubyforge.org/doc/en/cairo-color.html#label-5
19 20 21 |
# File 'lib/sources/color_source.rb', line 19 def self.named_colors @@named_colors ||= (Cairo::Color.constants.sort - %w{ RGB CMYK HSV X11 Base HEX_RE } - %w{ RGB CMYK HSV X11 Base HEX_RE }.map(&:to_sym)).map(&:to_s) end |
.parse(source) ⇒ Object
Returns source object or nil if it can’t parse it.
Supported syntax
All values can be given as Array
or String
separated by space chars.
To make color transparent add number value at the end of Array
or String
.
For any number value are valid values are 0 - 255 or 0.0 - 1.0
- Name of color
-
Use one of ColorSource.named_colors.
- RGB
-
Use separated number values for red, green and blue.
- CMYK
-
Use :cmyk key as first value followed by separated number values for cyan, magenta, yellow and black.
- HSV
-
Use :hsv key as first value followed by separated number values for hue, saturation and value.
- HEX
-
Use
String
starting with#
char followed by 6 or 3 hex numbers. Hex numbers are doubled if only 3 hex numbers are given. Color#AABBCC
is same as#ABC
.
Example
-
:red
is same as"red"
and[:red]
-
[255, 0, 0]
and"#FF0000"
makes red color -
[255, 0, 0, 64]
and["#F00", 64]
makes red color with 75% transparency -
[1.0, 0, 0, 0.25]
and"#F00 0.25"
makes red color with 75% transparency -
[:cmyk, 0, 0, 1.0, 0]
makes yellow color -
[:cmyk, 0, 0, 255, 0, 0.5]
makes yellow color with 50% transparency
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sources/color_source.rb', line 51 def self.parse(source) return source if source.is_a? SourceFactory if source[0].to_s =~ /^#([0-9a-f]{3}|[0-9a-f]{6})$/i hex = ($1.size == 6 ? $1 : $1.unpack('AXAAXAAXA').join).unpack("A2A2A2") source.shift hex.reverse.each {|h| source.unshift h.to_i(16) } end if is_all_nums(source, 0..2) treat_numbers source new Cairo::Color::RGB.new(*source[0..2]), source[3] elsif source[0] == "cmyk" && is_all_nums(source, 1..4) treat_numbers source new Cairo::Color::CMYK.new(*source[1..4]).to_rgb, source[5] elsif source[0] == "hsv" && is_all_nums(source, 1..3) treat_numbers source new Cairo::Color::HSV.new(*source[1..3]).to_rgb, source[4] elsif named_colors.include? source[0].to_s.upcase treat_numbers source new Cairo::Color.parse(source[0].to_s.upcase), source[1] end end |
Instance Method Details
#set_source(context, x, y, w, h) ⇒ Object
Sets color as source to given context
93 94 95 |
# File 'lib/sources/color_source.rb', line 93 def set_source(context, x, y, w, h) context.set_source_rgba @red, @green, @blue, @alpha end |