class Prawn::SVG::Color

Constants

DEFAULT_COLOR
HTML_COLORS
RGB_REGEXP
RGB_VALUE_REGEXP
URL_REGEXP

Public Class Methods

color_to_hex(color) click to toggle source
# File lib/prawn/svg/color.rb, line 211
def self.color_to_hex(color)
  result = parse(color).detect {|result| result.is_a?(Hex)}
  result.value if result
end
parse(color_string, gradients = nil) click to toggle source
# File lib/prawn/svg/color.rb, line 170
def self.parse(color_string, gradients = nil)
  url_specified = false

  components = color_string.to_s.strip.scan(/([^(\s]+(\([^)]*\))?)/)

  result = components.map do |color, *_|
    if m = color.match(/\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/i)
      Hex.new("#{m[1] * 2}#{m[2] * 2}#{m[3] * 2}")

    elsif color.match(/\A#[0-9a-f]{6}\z/i)
      Hex.new(color[1..6])

    elsif hex = HTML_COLORS[color.downcase]
      Hex.new(hex)

    elsif m = color.match(RGB_REGEXP)
      hex = (1..3).collect do |n|
        value = m[n].to_f
        value *= 2.55 if m[n][-1..-1] == '%'
        "%02x" % clamp(value.round, 0, 255)
      end.join

      Hex.new(hex)

    elsif matches = color.match(URL_REGEXP)
      url_specified = true
      url = matches[1]
      if url[0] == "#" && gradients && gradient = gradients[url[1..-1]]
        gradient
      end
    end
  end

  # Generally, we default to black if the colour was unparseable.
  # http://www.w3.org/TR/SVG/painting.html section 11.2 says if a URL was
  # supplied without a fallback, that's an error.
  result << DEFAULT_COLOR unless url_specified

  result.compact
end

Protected Class Methods

clamp(value, min_value, max_value) click to toggle source
# File lib/prawn/svg/color.rb, line 217
def self.clamp(value, min_value, max_value)
  [[value, min_value].max, max_value].min
end