Module: Sai::Conversion::RGB::ColorClassifier Private

Defined in:
lib/sai/conversion/rgb/color_classifier.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Classify color characteristics

Author:

Since:

  • 0.3.1

Class Method Summary collapse

Class Method Details

.closest_ansi_color(red, green, blue) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get closest ANSI color for RGB values

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Symbol) —

    the closest ANSI color name

Author:

Since:

  • 0.1.0



27
28
29
30
31
32
33
34
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 27

def closest_ansi_color(red, green, blue)
  return :black if dark?(red, green, blue)
  return :white if grayscale?(red, green, blue)
  return primary_color(red, green, blue) if primary?(red, green, blue)
  return secondary_color(red, green, blue) if secondary?(red, green, blue)

  :white
end

.dark?(red, green, blue) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if a color is dark

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Boolean) —

    true if color is dark

Author:

Since:

  • 0.1.0



49
50
51
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 49

def dark?(red, green, blue)
  [red, green, blue].max < 0.3
end

.grayscale?(red, green, blue) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if a color is grayscale

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Boolean) —

    true if color is grayscale

Author:

Since:

  • 0.1.0



66
67
68
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 66

def grayscale?(red, green, blue)
  red == green && green == blue
end

.primary?(red, green, blue) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if RGB values represent a primary color

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Boolean) —

    true if color is primary

Author:

Since:

  • 0.1.0



83
84
85
86
87
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 83

def primary?(red, green, blue)
  max = [red, green, blue].max
  mid = [red, green, blue].sort[1]
  (max - mid) > 0.3
end

.primary_color(red, green, blue) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the closest primary color

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Symbol) —

    the primary color name

Author:

Since:

  • 0.1.0



102
103
104
105
106
107
108
109
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 102

def primary_color(red, green, blue)
  max = [red, green, blue].max
  case max
  when red then :red
  when green then :green
  else :blue
  end
end

.secondary?(red, green, blue) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if RGB values represent a secondary color

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Boolean) —

    true if color is secondary

Author:

Since:

  • 0.1.0



124
125
126
127
128
129
130
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 124

def secondary?(red, green, blue)
  return true if yellow?(red, green, blue)
  return true if magenta?(red, green, blue)
  return true if cyan?(red, green, blue)

  false
end

.secondary_color(red, green, blue) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the closest secondary color

Parameters:

  • red (Float) —

    the red component (0-1)

  • green (Float) —

    the green component (0-1)

  • blue (Float) —

    the blue component (0-1)

Returns:

  • (Symbol) —

    the secondary color name

Author:

Since:

  • 0.1.0



145
146
147
148
149
150
151
# File 'lib/sai/conversion/rgb/color_classifier.rb', line 145

def secondary_color(red, green, blue)
  return :yellow if yellow?(red, green, blue)
  return :magenta if magenta?(red, green, blue)
  return :cyan if cyan?(red, green, blue)

  :white
end