Module: Colir::HSLRGB::RGB

Defined in:
lib/colir/hslrgb.rb

Overview

Provides helper methods for the RGB colours.

Since:

  • 0.0.1

Constant Summary collapse

RGB_RANGE =

The possible values of an RGB colour.

Since:

  • 0.0.1

0x000000..0xffffff
RGB_BYTE_RANGE =

The possible values of an RGB byte.

Since:

  • 0.0.1

0x00..0xff

Class Method Summary collapse

Class Method Details

.int_bytes(hex) ⇒ Array<Integer>

Converts hex number to the RGB array.

Examples:

RGB.int_bytes(0x123456) #=> [18, 52, 86]

Parameters:

  • hex (String)

    The hex number expressed without the preceding ‘0x`

Returns:

  • (Array<Integer>)

    the RGB array

Since:

  • 0.0.1



42
43
44
# File 'lib/colir/hslrgb.rb', line 42

def self.int_bytes(hex)
  hex.to_s(16).rjust(6, '0').scan(/../).map { |b| b.to_i(16) }
end

.valid_rgb?(rgb) ⇒ Boolean

Performs a validation check for the rgb.

Examples:

Truthy

RGB.valid_rgb?([255, 13, 0]) #=> true

Falsey

RGB.valid_rgb?([256, 13, -1]) #=> false

Parameters:

  • rgb (Array<Integer>)

    The RGB colour to be checked

Returns:

  • (Boolean)

    true if the given rgb colour lies within the ‘RGB_BYTE_RANGE`

Since:

  • 0.0.1



31
32
33
# File 'lib/colir/hslrgb.rb', line 31

def self.valid_rgb?(rgb)
  rgb.all? { |b| valid_byte?(b) } && (rgb.length == 3)
end