Class: SimpleColor::RGB

Inherits:
Object
  • Object
show all
Extended by:
ColorNames, Parsers, RGB16, Utils
Defined in:
lib/simplecolor/rgb.rb,
lib/simplecolor/rgb_colors.rb

Defined Under Namespace

Modules: ColorNames, Parsers, RGB16, Utils

Constant Summary collapse

GREY256 =
232
TRUE_COLOR =
0xFFFFFF
ANSI_COLORS_16 =
{
  :black  => 0,
  :red    => 1,
  :green  => 2,
  :yellow  => 3,
  :blue     => 4,
  :magenta => 5,
  :cyan     => 6,
  :white  => 7,
  :intense_black  => 8,
  :intense_red    => 9,
  :intense_green  => 10,
  :intense_yellow  => 11,
  :intense_blue     => 12,
  :intense_magenta => 13,
  :intense_cyan     => 14,
  :intense_white  => 15,
}
RGB_COLORS_ANSI =

A list of color names for standard ansi colors, needed for 16/8 color fallback mode See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors These are the xterm color palette

{
  :black  => [  0,   0,  0],
  :red    => [205,    0,   0],
  :green  => [  0, 205,   0],
  :yellow  => [205, 205,  0],
  :blue     => [  0,   0, 238],
  :magenta => [205,    0, 205],
  :cyan     => [  0, 205, 205],
  :white  => [229, 229, 229],
  :gray => [229, 229, 229],
}.each { |_k, v| v.freeze }.freeze
RGB_COLORS_ANSI_BRIGHT =
{
  :intense_black  => [127, 127, 127],
  :intense_red    => [255,    0,   0],
  :intense_green  => [  0, 255,   0],
  :intense_yellow  => [255, 255,  0],
  :intense_blue     => [ 92,  92, 255],
  :intense_magenta => [255,    0, 255],
  :intense_cyan     => [  0, 255, 255],
  :intense_white  => [255, 255, 255],
  :intense_gray => [255, 255, 255],
}.each { |_k, v| v.freeze }.freeze
RGB_COLORS_ANSI_16 =
RGB_COLORS_ANSI.merge(RGB_COLORS_ANSI_BRIGHT)
COLOR_NAMES =
{
  :random => proc { rgb_random },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsers

parse, rgb_hex

Methods included from Utils

color256_to_rgb, rgb_random, rgb_values

Methods included from RGB16

rgb_colors16, rgb_colors8

Methods included from ColorNames

all_color_names, color_names, color_names_priority, find_color, rgb_clean

Constructor Details

#initialize(*rgb, mode: :truecolor, background: false) ⇒ RGB

Returns a new instance of RGB.

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/simplecolor/rgb.rb', line 119

def initialize(*rgb, mode: :truecolor, background: false)
  raise WrongRGBColor.new(rgb) if rgb.empty?
  rgb=rgb.first if rgb.length==1
  raise WrongRGBColor.new(rgb) if rgb.nil?

  @init=rgb
  @color=rgb #should be an array for truecolor, a number otherwise
  @mode=color_mode(mode)
  @background=!!background
  
  case @mode
  when :truecolor
    unless @color&.size == 3 && @color&.all?{ |n| n.is_a? Numeric }
      raise WrongRGBColor.new(rgb)
    end
    raise WrongRGBColor.new(rgb) unless rgb.all? do |c|
      (0..255).include?(c)
    end
  when 256 #for 256 colors we are more lenient
    case rgb
    when Array
      unless @color&.size == 3 && @color&.all?{ |n| n.is_a? Numeric }
        raise WrongRGBColor.new(rgb)
      end
      raise WrongRGBColor.new(rgb) unless rgb.all? do |c|
        (0..5).include?(c)
      end
      red, green, blue=rgb
      @color=16 + 36 * red.to_i + 6 * green.to_i + blue.to_i
    when String, Symbol #for grey mode
      if (m=rgb.to_s.match(/\Agr[ae]y(\d+)\z/))
        @color=GREY256+m[1].to_i
      else
        raise WrongRGBColor.new(rgb)
      end
    else
      raise WrongRGBColor.new(rgb) unless @color.is_a?(Numeric)
    end
  when 8,16
    @color=ANSI_COLORS_16[rgb] if ANSI_COLORS_16.key?(rgb)
    raise WrongRGBColor.new(rgb) unless @color.is_a?(Numeric)
  end
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



103
104
105
# File 'lib/simplecolor/rgb.rb', line 103

def background
  @background
end

#colorObject

Returns the value of attribute color.



103
104
105
# File 'lib/simplecolor/rgb.rb', line 103

def color
  @color
end

#modeObject

Returns the value of attribute mode.



103
104
105
# File 'lib/simplecolor/rgb.rb', line 103

def mode
  @mode
end

Instance Method Details

#==(other) ⇒ Object



163
164
165
166
167
# File 'lib/simplecolor/rgb.rb', line 163

def ==(other)
  [:color, :mode, :background].all? do |sym|
    self.public_send(sym) == other.public_send(sym)
  end
end

#ansi(background: @background, convert: nil) ⇒ Object

For true colors: ESC[ 38;2;;; m Select RGB foreground color ESC[ 48;2;;; m Select RGB background color



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/simplecolor/rgb.rb', line 199

def ansi(background: @background, convert: nil)
  return self.convert(convert, only_down: true).ansi(background: background, convert: nil) if convert
  case @mode
  when 8, 16
    if @color < 8
      "#{background ? 4 : 3}#{@color}"
    else
      # Note that on_intense_* is not supported by terminals
      # it is usually better to convert to 256 colors palette which is
      # better supported
      "#{background ? 10 : 9}#{@color-8}"
    end
  when 256
    "#{background ? 48 : 38};5;#{@color}"
  when :truecolor
    red, green, blue=@color
    "#{background ? 48 : 38};2;#{red};#{green};#{blue}"
  end
end

#convert(mode, only_down: false) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/simplecolor/rgb.rb', line 219

def convert(mode, only_down: false)
  case color_mode(mode)
  when 8
    return to_8
  when 16
    return to_16 unless only_down and nbcolors < 16
  when 256
    return to_256 unless only_down and nbcolors < 256
  when :truecolor
    return to_truecolor unless only_down and nbcolors < TRUE_COLOR
  end
  self
end

#nbcolorsObject



173
174
175
176
# File 'lib/simplecolor/rgb.rb', line 173

def nbcolors
  return TRUE_COLOR if @mode == :truecolor
  return @mode
end

#rgbObject



184
185
186
# File 'lib/simplecolor/rgb.rb', line 184

def rgb
  to_truecolor.color
end

#rgb_color_distance(rgb2) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/simplecolor/rgb.rb', line 302

def rgb_color_distance(rgb2)
  if truecolor?
    @color.zip(self.class.rgb_values(rgb2)).inject(0){ |acc, (cur1, cur2)| acc + (cur1 - cur2)**2 }
  else
    to_truecolor.rgb_color_distance(rgb2)
  end
end

#rgb_to_pool(color_pool) ⇒ Object



310
311
312
313
314
315
316
# File 'lib/simplecolor/rgb.rb', line 310

def rgb_to_pool(color_pool)
  if truecolor?
    color_pool.min_by{ |col| rgb_color_distance(col) }
  else
    to_truecolor.rgb_to_pool(color_pool)
  end
end

#to_16Object



294
295
296
297
298
299
300
# File 'lib/simplecolor/rgb.rb', line 294

def to_16
  rgb16=self.class.rgb_colors16
  color_pool = rgb16.values
  closest=rgb_to_pool(color_pool)
  name=rgb16.key(closest)
  self.class.new(ANSI_COLORS_16[name], mode: 16)
end

#to_256Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/simplecolor/rgb.rb', line 254

def to_256
  case @mode
  when 256
    return self
  when 8, 16
    return self.class.new(@color, mode: 256)
  else
    red,green,blue=@color

    gray_possible = true
    sep = 42.5

    while gray_possible
      if red < sep || green < sep || blue < sep
        gray = red < sep && green < sep && blue < sep
        gray_possible = false
      end
      sep += 42.5
    end

    col=if gray
      GREY256 + ((red.to_f + green.to_f + blue.to_f)/33).round
    else # rgb
      [16, *[red, green, blue].zip([36, 6, 1]).map{ |color, mod|
        (6 * (color.to_f / 256)).to_i * mod
      }].inject(:+)
    end
    self.class.new(col, mode: 256)

  end
end

#to_8Object



286
287
288
289
290
291
292
# File 'lib/simplecolor/rgb.rb', line 286

def to_8
  rgb8=self.class.rgb_colors8
  color_pool = rgb8.values
  closest=rgb_to_pool(color_pool)
  name=rgb8.key(closest)
  self.class.new(ANSI_COLORS_16[name], mode: 8)
end

#to_hexObject



178
179
180
181
182
# File 'lib/simplecolor/rgb.rb', line 178

def to_hex
  r,g,b=rgb
  hexa = ->(c) {h=c.to_s(16).upcase; h.length == 1 ? "0#{h}" : h}
  "\##{hexa[r]}#{hexa[g]}#{hexa[b]}"
end

#to_truecolorObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/simplecolor/rgb.rb', line 233

def to_truecolor
  case @mode
  when 8, 16
    name=ANSI_COLORS_16.key(@color)
    rgb=self.class.rgb_colors16[name]
    self.class.new(rgb)
  when 256
    if @color < 16
      to_16.to_truecolor
    elsif @color < GREY256
      red, green, blue=self.class.color256_to_rgb(@color)
      self.class.new([red, green, blue].map {|c| (c * 256.0/7.0).round})
    else
      grey=@color-GREY256
      self.class.new([(grey*256.0/24.0).round]*3)
    end
  else
    self
  end
end

#truecolor?Boolean



169
170
171
# File 'lib/simplecolor/rgb.rb', line 169

def truecolor?
  @mode == :truecolor
end