module

CrImage::Util::PaletteExtractor

Extracts dominant colors from images for theming, analysis, and design.

Provides tools for analyzing color composition and extracting the most prominent colors from images. Useful for:

  • Generating color themes and palettes
  • Image categorization and search
  • Design tools and color pickers
  • Automatic UI theming
  • Color-based image analysis

Class methods

dominant_color(src : Image) : Color::Color

Extracts the single most dominant color from the image.

Convenience method that returns only the most prominent color.

Parameters:

  • src : The source image

Returns: The most dominant color

Example:

img = CrImage.read("photo.jpg")
dominant = img.dominant_color
puts "Main color: #{dominant.to_hex}"
Source
extract(src : Image, count : Int32 = 5, algorithm : QuantizationAlgorithm = QuantizationAlgorithm::MedianCut) : Array(Color::Color)

Extracts the most dominant colors from an image.

Uses color quantization algorithms to identify the most prominent colors. Returns colors sorted by visual prominence (most dominant first).

Parameters:

  • src : The source image
  • count : Number of colors to extract (1-256, default: 5)
  • algorithm : Quantization algorithm to use (default: MedianCut)

Returns: Array of dominant colors, sorted by prominence

Raises: ArgumentError if count is outside 1-256 range

Example:

img = CrImage.read("photo.jpg")
colors = img.extract_palette(5)
colors.each { |c| puts c.to_hex }
Source
extract_with_weights(src : Image, count : Int32 = 5, algorithm : QuantizationAlgorithm = QuantizationAlgorithm::MedianCut) : Array(Tuple(Color::Color, Float64))

Extracts dominant colors with their relative frequencies.

Similar to extract but also returns the percentage of the image each color represents. Results are sorted by frequency (most common first).

Parameters:

  • src : The source image
  • count : Number of colors to extract (1-256, default: 5)
  • algorithm : Quantization algorithm to use (default: MedianCut)

Returns: Array of tuples (color, weight) where weight is 0.0-1.0

Raises: ArgumentError if count is outside 1-256 range

Example:

img = CrImage.read("photo.jpg")
colors = img.extract_palette_with_weights(5)
colors.each do |color, weight|
  puts "#{color.to_hex}: #{(weight * 100).round(1)}%"
end
Source