Class: EnvStyle::RasterTinter

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

Overview

Tints a raster icon (PNG/ICO/...) with libvips through ruby-vips.

The recolor is luminance-preserving: each pixel's perceptual brightness scales the tint color, so shading survives while the hue shifts. Alpha is carried through untouched, and any pixel within tolerance of a color in exclude_colors (for example a white background) is left exactly as it was. A tolerance of 0 keeps only exact matches; a larger value also preserves the anti-aliased halo of near-matching pixels around an excluded region.

vips is required lazily so SVG-only applications never need libvips.

Constant Summary collapse

CONTENT_TYPE =
"image/png"

Instance Method Summary collapse

Constructor Details

#initialize(color:, exclude_colors: [], tolerance: 0) ⇒ RasterTinter

Returns a new instance of RasterTinter.



17
18
19
20
21
# File 'lib/env_style/raster_tinter.rb', line 17

def initialize(color:, exclude_colors: [], tolerance: 0)
  @color = color.to_s
  @exclude_colors = Array(exclude_colors)
  @tolerance = tolerance
end

Instance Method Details

#call(bytes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/env_style/raster_tinter.rb', line 27

def call(bytes)
  require "vips"

  image = Vips::Image.new_from_buffer(bytes, "").colourspace("srgb")
  alpha = image.bands >= 4 ? image.extract_band(3) : nil
  rgb = image.extract_band(0, n: 3)

  recolored = apply_exclusions(rgb, tint(rgb))
  recolored = recolored.bandjoin(alpha) if alpha
  recolored.cast(:uchar).copy(interpretation: :srgb).write_to_buffer(".png")
end

#content_typeObject



23
24
25
# File 'lib/env_style/raster_tinter.rb', line 23

def content_type
  CONTENT_TYPE
end