Class: Colir

Inherits:
Object
  • Object
show all
Defined in:
lib/colir.rb,
lib/colir/hslrgb.rb

Overview

This tiny library provides support for RGB colours and some simple manipulations with them. See README.md for more information.

Defined Under Namespace

Modules: HSLRGB

Constant Summary collapse

VERSION_FILE =

The VERSION file must be in the root directory of the library.

File.expand_path('../../VERSION', __FILE__)
VERSION =
File.exist?(VERSION_FILE) ?
File.read(VERSION_FILE).chomp : '(could not find VERSION file)'
COLIRS =

The list for human readable colour according to the HTML and CSS color specification. Does not include alpha channel.

{
  alice_blue:              0xf0f8ff,
  antique_white:           0xfaebd7,
  aqua:                    0x00ffff,
  aquamarine:              0x7fffd4,
  azure:                   0xf0ffff,
  beige:                   0xf5f5dc,
  bisque:                  0xffe4c4,
  black:                   0x000000,
  blanched_almond:         0xffebcd,
  blue:                    0x0000ff,
  blue_violet:             0x8a2be2,
  brown:                   0xa52a2a,
  burly_wood:              0xdeb887,
  cadet_blue:              0x5f9ea0,
  chartreuse:              0x7fff00,
  chocolate:               0xd2691e,
  coral:                   0xff7f50,
  cornflower_blue:         0x6495ed,
  cornsilk:                0xfff8dc,
  crimson:                 0xdc143c,
  cyan:                    0x00ffff,
  dark_blue:               0x00008b,
  dark_cyan:               0x008b8b,
  dark_golden_rod:         0xb8860b,
  dark_gray:               0xa9a9a9,
  dark_green:              0xa9a9a9,
  dark_khaki:              0xbdb76b,
  dark_magenta:            0x8b008b,
  dark_olive_green:        0x556b2f,
  dark_orange:             0xff8c00,
  dark_orchid:             0x9932cc,
  dark_red:                0x9932cc,
  dark_salmon:             0xe9967a,
  dark_sea_green:          0x8fbc8f,
  dark_slate_blue:         0x483d8b,
  dark_slate_gray:         0x2f4f4f,
  dark_turquoise:          0x00ced1,
  dark_violet:             0x9400d3,
  deep_pink:               0xff1493,
  deep_sky_blue:           0x00bfff,
  dim_gray:                0x696969,
  dim_grey:                0x696969,
  dodger_blue:             0x1e90ff,
  fire_brick:              0xb22222,
  floral_white:            0xfffaf0,
  forest_green:            0x228b22,
  fuchsia:                 0xff00ff,
  gainsboro:               0xdcdcdc,
  ghost_white:             0xf8f8ff,
  gold:                    0xffd700,
  golden_rod:              0xdaa520,
  gray:                    0x808080,
  green:                   0x008000,
  green_yellow:            0xadff2f,
  honey_dew:               0xf0fff0,
  hot_pink:                0xff69b4,
  indian_red:              0xcd5c5c,
  indigo:                  0x4b0082,
  ivory:                   0xfffff0,
  khaki:                   0xf0e68c,
  lavender:                0xe6e6fa,
  lavender_blush:          0xfff0f5,
  lawn_green:              0x7cfc00,
  lemon_chiffon:           0xfffacd,
  light_blue:              0xadd8e6,
  light_coral:             0xf08080,
  light_cyan:              0xe0ffff,
  light_golden_rod_yellow: 0xfafad2,
  light_gray:              0xd3d3d3,
  light_green:             0x90ee90,
  light_pink:              0xffb6c1,
  light_salmon:            0xffa07a,
  light_sea_green:         0x20b2aa,
  light_sky_blue:          0x87cefa,
  light_slate_gray:        0x778899,
  light_steel_blue:        0xb0c4de,
  light_yellow:            0xffffe0,
  lime:                    0x00ff00,
  lime_green:              0x32cd32,
  linen:                   0xfaf0e6,
  magenta:                 0xff00ff,
  maroon:                  0x800000,
  medium_aqua_marine:      0x66cdaa,
  medium_blue:             0x0000cd,
  medium_orchid:           0xba55d3,
  medium_purple:           0x9370db,
  medium_sea_green:        0x3cb371,
  medium_slate_blue:       0x7b68ee,
  medium_spring_green:     0x00fa9a,
  medium_turquoise:        0x48d1cc,
  medium_violet_red:       0xc71585,
  midnight_blue:           0x191970,
  mint_cream:              0xf5fffa,
  misty_rose:              0xffe4e1,
  moccasin:                0xffe4b5,
  navajo_white:            0xffdead,
  navy:                    0x000080,
  old_lace:                0xfdf5e6,
  olive:                   0x808000,
  olive_drab:              0x6b8e23,
  orange:                  0xffa500,
  orange_red:              0xff4500,
  orchid:                  0xda70d6,
  pale_golden_rod:         0xeee8aa,
  pale_green:              0x98fb98,
  pale_turquoise:          0xafeeee,
  pale_violet_red:         0xdb7093,
  papaya_whip:             0xffefd5,
  peach_puff:              0xffdab9,
  peru:                    0xcd853f,
  pink:                    0xffc0cb,
  plum:                    0xdda0dd,
  powder_blue:             0xb0e0e6,
  purple:                  0x800080,
  red:                     0xff0000,
  rosy_brown:              0xbc8f8f,
  royal_blue:              0x4169e1,
  saddle_brown:            0x8b4513,
  salmon:                  0xfa8072,
  sandy_brown:             0xf4a460,
  sea_green:               0x2e8b57,
  sea_shell:               0xfff5ee,
  sienna:                  0xa0522d,
  silver:                  0xc0c0c0,
  sky_blue:                0x87ceeb,
  slate_blue:              0x6a5acd,
  slate_gray:              0x708090,
  snow:                    0xfffafa,
  spring_green:            0x00ff7f,
  steel_blue:              0x468284,
  tan:                     0xd2b48c,
  teal:                    0x008080,
  thistle:                 0xd8bfd8,
  tomato:                  0xff6347,
  turquoise:               0x40e0d0,
  violet:                  0xee82ee,
  wheat:                   0xf5debe,
  white:                   0x000000,
  white_smoke:             0xf5f5f5,
  yellow:                  0xffff00,
  yellow_green:            0x9acd32
}
SHADE_FACTOR =

The possible number of shades of a Colir in each direction.

5
TRANSPARENCY =

The default transparency (opaque).

0.0
SHADE =

The default shade assigned to any Colir.

0
UPPER_LIMIT =
BigDecimal(HSLRGB::HSL::L_RANGE.max.to_f.to_s)
RGB_LOWER_LIMIT =

The mi

0x000000
RGB_UPPER_LIMIT =
0xffffff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex, transparency = TRANSPARENCY) ⇒ Colir

Creates a new HEXA colour, where the A states for “Alpha”.

Parameters:

  • It’s convenient to use Ruby’s HEX notation: 0xaaff33

  • (defaults to: TRANSPARENCY)

    Alpha channel. Must lie within the range of [0, 1].

Raises:

  • if the transparency is a bad value



216
217
218
219
220
221
222
223
# File 'lib/colir.rb', line 216

def initialize(hex, transparency = TRANSPARENCY)
  validate_colir!(hex, transparency)

  @hex = hex
  @transparency = transparency
  @shade = SHADE
  @ld_seq = [:base]
end

Instance Attribute Details

#hexInteger (readonly)

Returns the HEX colour without the alpha channel.

Returns:

  • the HEX colour without the alpha channel



201
202
203
# File 'lib/colir.rb', line 201

def hex
  @hex
end

#shadeInteger (readonly)

Returns the current shade of the colour. Lies within the range of [-5, 5].

Returns:

  • the current shade of the colour. Lies within the range of [-5, 5]



208
209
210
# File 'lib/colir.rb', line 208

def shade
  @shade
end

#transparencyFloat

Returns the transparency. Lies within the range of [0, 1].

Returns:

  • the transparency. Lies within the range of [0, 1].



204
205
206
# File 'lib/colir.rb', line 204

def transparency
  @transparency
end

Class Method Details

.nameColir

Dynamically define class methods. Each human readable colour turns into a method. It’s useful if you don’t care about precise colours or can’t remember HEX codes for a colour.

Examples:

Accessing a colour

Colir.wheat.hex #=> 0xf5debe00

Transparency

Colir.wheat(0.3).hex #=> 0xf5debee1

Parameters:

Returns:

See Also:



176
177
178
179
180
# File 'lib/colir.rb', line 176

COLIRS.each do |name, hex|
  define_method(name) { |transparency = nil|
    new(hex, transparency || TRANSPARENCY)
  }
end

Instance Method Details

#darkenself

Darkens self by 1 shade. The maximum number of negative shades is -5. The method stops decreasing the shade levels when it reaches the limit and merely returns self without any changes. The final shade is black colour.

Returns:

  • the darkened self



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/colir.rb', line 282

def darken
  if SHADE_FACTOR * -1 < @shade
    calculate_new_hex { |hsl|
      hsl[2] -= if @ld_seq.last == :base || @ld_seq.last == :dkn
                  @ld_seq << :dkn
                  d_factor
                else
                  @ld_seq.pop
                  l_factor
                end
      @shade -= 1
      hsl
    }
  end
  self
end

#darkerColir

Returns a new Colir, but darker than self by 1 shade. It does not modify self.

Returns:

  • a new Colir, but darker than self by 1 shade. It does not modify self



308
309
310
# File 'lib/colir.rb', line 308

def darker
  self.class.new(@hex, @transparency).darken
end

#hexaInteger

Returns the HEXA representation of the Colir.

Returns:

  • the HEXA representation of the Colir



226
227
228
229
230
# File 'lib/colir.rb', line 226

def hexa
  hex = @hex.to_s(16).rjust(6, '0')
  alpha = (transparency * 100).to_i.to_s(16).rjust(2, '0')
  (hex + alpha).to_i(16)
end

#lightenself

Lightens self by 1 shade. The maximum number of positive shades is 5. The method stops increasing the shade levels when it reaches the limit and merely returns self without any changes. The final shade is white colour.

Returns:

  • the lightened self



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/colir.rb', line 260

def lighten
  if @shade < SHADE_FACTOR
    calculate_new_hex { |hsl|
      hsl[2] += if @ld_seq.last == :base || @ld_seq.last == :ltn
                  @ld_seq << :ltn
                  l_factor
                else
                  @ld_seq.pop
                  d_factor
                end
      @shade += 1
      hsl
    }
  end
  self
end

#lighterColir

Returns a new Colir, but lighter than self by 1 shade. It does not modify self.

Returns:

  • a new Colir, but lighter than self by 1 shade. It does not modify self



314
315
316
# File 'lib/colir.rb', line 314

def lighter
  self.class.new(@hex, @transparency).lighten
end

#opaque!self

Make the colour fully opaque.

Returns:



250
251
252
253
# File 'lib/colir.rb', line 250

def opaque!
  @transparency = TRANSPARENCY
  self
end

#reset_shadeself

Resets the ‘@shade` to the default value, restoring the `@hex` number.

Returns:



301
302
303
304
# File 'lib/colir.rb', line 301

def reset_shade
  @shade.abs.times { @ld_seq.last == :ltn ? darken : lighten }
  self
end

#transparent!self

Make the colour fully transparent.

Returns:



234
235
236
237
# File 'lib/colir.rb', line 234

def transparent!
  @transparency = 1.0
  self
end