Class: Arrolio::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/color.rb

Overview

Color value object. Parses CSS/XSL color specifications and provides normalized RGB components for the renderer. Supports: hex (#RGB, #RRGGBB), rgb(r,g,b), rgb(r%,g%,b%), named colors.

The renderer calls to_render to get the format pdfrb expects: Float for grayscale, [:rgb, r, g, b] for color.

Constant Summary collapse

NAMED_COLORS =

Named colors from the SVG/CSS specification subset commonly used in XSL-FO documents.

{
  'black' => [0, 0, 0],
  'white' => [255, 255, 255],
  'red' => [255, 0, 0],
  'green' => [0, 128, 0],
  'blue' => [0, 0, 255],
  'yellow' => [255, 255, 0],
  'cyan' => [0, 255, 255],
  'magenta' => [255, 0, 255],
  'gray' => [128, 128, 128],
  'grey' => [128, 128, 128],
  'silver' => [192, 192, 192],
  'maroon' => [128, 0, 0],
  'purple' => [128, 0, 128],
  'olive' => [128, 128, 0],
  'navy' => [0, 0, 128],
  'teal' => [0, 128, 128]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red:, green:, blue:, alpha: 1.0) ⇒ Color

Returns a new instance of Color.



34
35
36
37
38
39
40
# File 'lib/arrolio/color.rb', line 34

def initialize(red:, green:, blue:, alpha: 1.0)
  @red = clamp01(red.to_f)
  @green = clamp01(green.to_f)
  @blue = clamp01(blue.to_f)
  @alpha = clamp01(alpha.to_f)
  freeze
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



11
12
13
# File 'lib/arrolio/color.rb', line 11

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



11
12
13
# File 'lib/arrolio/color.rb', line 11

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



11
12
13
# File 'lib/arrolio/color.rb', line 11

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



11
12
13
# File 'lib/arrolio/color.rb', line 11

def red
  @red
end

Class Method Details

.parse(spec) ⇒ Object

Factory: parse any supported color specification.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arrolio/color.rb', line 43

def self.parse(spec)
  return spec if spec.is_a?(self)
  return nil if spec.nil? || spec.to_s.empty?

  case spec.to_s.downcase
  when /\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/
    new(red: Regexp.last_match(1).to_i(16) * 17 / 255.0,
        green: Regexp.last_match(2).to_i(16) * 17 / 255.0,
        blue: Regexp.last_match(3).to_i(16) * 17 / 255.0)
  when /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/
    new(red: Regexp.last_match(1).to_i(16) / 255.0,
        green: Regexp.last_match(2).to_i(16) / 255.0,
        blue: Regexp.last_match(3).to_i(16) / 255.0)
  when /\Argb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\z/
    new(red: Regexp.last_match(1).to_i / 255.0,
        green: Regexp.last_match(2).to_i / 255.0,
        blue: Regexp.last_match(3).to_i / 255.0)
  when /\Argb\(\s*([\d.]+)%\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)\z/
    new(red: Regexp.last_match(1).to_f / 100.0,
        green: Regexp.last_match(2).to_f / 100.0,
        blue: Regexp.last_match(3).to_f / 100.0)
  else
    parse_named(spec.to_s.downcase)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



90
91
92
93
94
# File 'lib/arrolio/color.rb', line 90

def ==(other)
  other.is_a?(self.class) &&
    red == other.red && green == other.green &&
    blue == other.blue && alpha == other.alpha
end

#grayscale?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/arrolio/color.rb', line 82

def grayscale?
  @red == @green && @green == @blue
end

#hashObject



97
98
99
# File 'lib/arrolio/color.rb', line 97

def hash
  [self.class, red, green, blue, alpha].hash
end

#to_grayscale_floatObject



86
87
88
# File 'lib/arrolio/color.rb', line 86

def to_grayscale_float
  (0.299 * @red) + (0.587 * @green) + (0.114 * @blue)
end

#to_renderObject

Returns the format pdfrb's canvas expects: [:rgb, r, g, b].



78
79
80
# File 'lib/arrolio/color.rb', line 78

def to_render
  [:rgb, @red, @green, @blue]
end