class Prawn::SVG::Color

Constants

CMYK
CMYK_DEFAULT_COLOR
HTML_COLORS
RGB
RGB_DEFAULT_COLOR

Public Class Methods

css_color_to_prawn_color(color) click to toggle source
# File lib/prawn/svg/color.rb, line 214
def css_color_to_prawn_color(color)
  parse(color).detect { |value| value.is_a?(RGB) || value.is_a?(CMYK) }&.value
end
default_color(color_mode) click to toggle source
# File lib/prawn/svg/color.rb, line 218
def default_color(color_mode)
  color_mode == :cmyk ? CMYK_DEFAULT_COLOR : RGB_DEFAULT_COLOR
end
parse(color_string, gradients = nil, color_mode = :rgb) click to toggle source
# File lib/prawn/svg/color.rb, line 159
def parse(color_string, gradients = nil, color_mode = :rgb)
  url_specified = false

  values = ::Prawn::SVG::CSS::ValuesParser.parse(color_string)

  result = values.map do |value|
    case value
    in ['rgb', args]
      hex = (0..2).collect do |n|
        number = args[n].to_f
        number *= 2.55 if args[n][-1..] == '%'
        format('%02x', number.round.clamp(0, 255))
      end.join

      RGB.new(hex)

    in ['device-cmyk', args]
      cmyk = (0..3).collect do |n|
        number = args[n].to_f
        number *= 100 unless args[n][-1..] == '%'
        number.clamp(0, 100)
      end

      CMYK.new(cmyk)

    in ['url', [url]]
      url_specified = true
      if url[0] == '#' && gradients && (gradient = gradients[url[1..]])
        gradient
      end

    in /\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/i
      RGB.new("#{$1 * 2}#{$2 * 2}#{$3 * 2}")

    in /\A#[0-9a-f]{6}\z/i
      RGB.new(value[1..])

    in String => color
      if (hex = HTML_COLORS[color.downcase])
        hex_color(hex, color_mode)
      end

    else
      nil
    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(color_mode) unless url_specified

  result.compact
end

Private Class Methods

hex_color(hex, color_mode) click to toggle source
# File lib/prawn/svg/color.rb, line 224
def hex_color(hex, color_mode)
  if color_mode == :cmyk
    r, g, b = [hex[0..1], hex[2..3], hex[4..5]].map { |h| h.to_i(16) / 255.0 }
    k = 1 - [r, g, b].max
    if k == 1
      CMYK.new([0, 0, 0, 100])
    else
      c = (1 - r - k) / (1 - k)
      m = (1 - g - k) / (1 - k)
      y = (1 - b - k) / (1 - k)
      CMYK.new([c, m, y, k].map { |v| (v * 100).round })
    end
  else
    RGB.new(hex)
  end
end