Class: EnvStyle::SvgTinter

Inherits:
Object
  • Object
show all
Defined in:
lib/env_style/svg_tinter.rb

Overview

Tints an SVG icon by rewriting its markup — no image library required.

A monochrome mark (one color, or currentColor, or an implicit default) has its fill/stroke swapped for the tint. A multicolor mark is wrapped in a group carrying a luminance-preserving duotone filter, so the artwork keeps its shading while shifting to the tint hue.

Constant Summary collapse

CONTENT_TYPE =
"image/svg+xml"
FILTER_ID =
"env-style-tint"
NON_COLORS =

Values that are not real colors and must never be swapped.

["none", "transparent", "inherit", "currentcolor", ""].freeze
CSS_COLORS =

Common CSS named colors mapped to canonical 6-digit hex, so that a mark written as "black" and "#000" is recognised as one color when counting distinctness. Used only for the count — never for the output markup.

{
  "aqua" => "#00ffff",
  "black" => "#000000",
  "blue" => "#0000ff",
  "cyan" => "#00ffff",
  "fuchsia" => "#ff00ff",
  "gray" => "#808080",
  "green" => "#008000",
  "grey" => "#808080",
  "lime" => "#00ff00",
  "magenta" => "#ff00ff",
  "maroon" => "#800000",
  "navy" => "#000080",
  "olive" => "#808000",
  "orange" => "#ffa500",
  "purple" => "#800080",
  "red" => "#ff0000",
  "silver" => "#c0c0c0",
  "teal" => "#008080",
  "white" => "#ffffff",
  "yellow" => "#ffff00"
}.freeze
LUMINANCE =

Luminance weights (Rec. 709) used to preserve shading in the duotone.

[0.2126, 0.7152, 0.0722].freeze
STYLE_VALUE =

A CSS color value inside a style declaration: a hex, a functional notation (which may contain spaces/commas), or a plain token. Anchored so it never runs past the declaration's ; or the attribute's closing quote.

/#[0-9a-fA-F]+|(?:rgb|hsl)a?\([^)]*\)|[^;"'\s)]+/

Instance Method Summary collapse

Constructor Details

#initialize(color:) ⇒ SvgTinter

Returns a new instance of SvgTinter.



51
52
53
# File 'lib/env_style/svg_tinter.rb', line 51

def initialize(color:)
  @color = color.to_s
end

Instance Method Details

#call(svg) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/env_style/svg_tinter.rb', line 59

def call(svg)
  svg = svg.to_s
  colors = distinct_colors(svg)
  if colors.size >= 2
    duotone(svg, colored: colors.any?)
  else
    monochrome(svg, colored: colors.any?)
  end
end

#content_typeObject



55
56
57
# File 'lib/env_style/svg_tinter.rb', line 55

def content_type
  CONTENT_TYPE
end