Module: RGB

Defined in:
lib/redgreenblue.rb,
lib/redgreenblue/gpl.rb,
lib/redgreenblue/hex.rb,
lib/redgreenblue/hsb.rb,
lib/redgreenblue/hsl.rb,
lib/redgreenblue/hsv.rb,
lib/redgreenblue/int.rb,
lib/redgreenblue/web.rb,
lib/redgreenblue/base.rb,
lib/redgreenblue/lazy.rb,
lib/redgreenblue/misc.rb,
lib/redgreenblue/24bit.rb,
lib/redgreenblue/48bit.rb,
lib/redgreenblue/os/mac.rb,
lib/redgreenblue/random.rb,
lib/redgreenblue/rgb565.rb,
lib/redgreenblue/inspect.rb,
lib/redgreenblue/version.rb,
lib/redgreenblue/bgr24bit.rb,
lib/redgreenblue/cie_1931.rb

Overview

The RGB module is the main namespace for redgreenblue.

This module has many helper methods for creating RGB::Color objects. The example below shows seven ways to create an RGB::Color object for the exact same color.

Examples:

require 'redgreenblue'

pink = RGB.new(1, 0.6, 0.8)
pink = RGB.rgb(255, 153, 204)
pink = RGB.rrggbb(65535, 39321, 52428)
pink = RGB.hex('f9c')
pink = RGB.hsl(330, 1, 0.8)
pink = RGB.hsv(330, 0.4, 1)
pink = RGB.at(16751052)

Defined Under Namespace

Classes: Color

Constant Summary collapse

VERSION =

redgreenblue version.

'0.19.0'

Class Method Summary collapse

Class Method Details

.assemble(*a) ⇒ Object

Creates a new RGB::Color from three RGB::Colors representing the red, green, and blue components.



60
61
62
# File 'lib/redgreenblue/misc.rb', line 60

def assemble(*a)
  Color.assemble(*a)
end

.at(number) ⇒ Object

Creates a new RGB::Color from a 24-bit integer in the range 0..16777215.



30
31
32
# File 'lib/redgreenblue/int.rb', line 30

def at(number)
  Color.at(number)
end

.base_styleObject

Returns the base inspect style, dependent on the COLORTERM environment variable.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/redgreenblue/inspect.rb', line 62

def base_style
  if styles.include? ENV['REDGREENBLUE_STYLE']
    ENV['REDGREENBLUE_STYLE']
  else
    if ENV['COLORTERM'] == 'truecolor'
      'simple'
    else
      'default'
    end
  end
end

.bgr24(bgr) ⇒ Object

Creates a new RGB::Color from BGR24 data (a 3-byte string).



28
29
30
# File 'lib/redgreenblue/bgr24bit.rb', line 28

def bgr24(bgr)
  Color.bgr24(bgr)
end

.blackObject

Creates a black RGB::Color.



83
84
85
# File 'lib/redgreenblue/lazy.rb', line 83

def black
  Color.black
end

.blueObject

Creates a pure blue RGB::Color.



108
109
110
# File 'lib/redgreenblue/lazy.rb', line 108

def blue
  Color.blue
end

.centreObject Also known as: center

Returns the centre of the RGB cube.



133
134
135
# File 'lib/redgreenblue/lazy.rb', line 133

def centre
  grey
end

.cie_xyz(*a) ⇒ Object Also known as: xyz

Creates a new RGB::Color from CIE 1931 XYZ values.

Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.



91
92
93
# File 'lib/redgreenblue/cie_1931.rb', line 91

def cie_xyz(*a)
  RGB::Color.cie_xyz(a)
end

.cornersObject

Returns the 8 corners of the RGB cube.



128
129
130
# File 'lib/redgreenblue/lazy.rb', line 128

def corners
  [ black, red, yellow, green, cyan, blue, magenta, white ]
end

.css(selector = nil) ⇒ Object

Returns CSS named colors, as per CSS Color Module Level 4.

Optional selector argument can be:

  • String or Regexp (to select by name)

  • RGB color (to select by color)

  • Integer (to select by index).

Selection by name (string or regular expression) is case-insensitive by default.

Examples:

No Selector

# All colors
RGB.css

# Pastels
RGB.css.select { |c| c.ostwald_cwk[1] > 0.6 }

Select by Name

# Exact name
RGB.css 'pink'

# Regular expression
RGB.css /pink|rose/

Select by RGB color

RGB.css RGB.hex('0ff')


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/redgreenblue/web.rb', line 89

def css(selector=nil)
  @@css ||= load_gpl file: ( File.join File.dirname(__FILE__), 'palettes', 'css.gpl' ), freeze: true
  case selector
    when NilClass
      @@css
    when String
      n = selector.downcase
      @@css.select { |c| c.name == n }.first
    when Regexp
      r = Regexp.new selector.source, Regexp::IGNORECASE
      @@css.select { |c| c.name =~ r }
    when Integer
      @@css[selector]
    when RGB::Color
      @@css.select { |c| c == selector }
    else
      raise ArgumentError, 'Unsupported selector'
  end
end

.cyanObject

Creates a cyan RGB::Color.



118
119
120
# File 'lib/redgreenblue/lazy.rb', line 118

def cyan
  Color.cyan
end

.each_24bit_colorObject

Calls the given block for each 24-bit RGB color (from black to white), passing the color as an RGB::Color object.

Returns the number of iterations.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redgreenblue/24bit.rb', line 80

def each_24bit_color
  range = 0..255
  range.each do |r|
    range.each do |g|
      range.each do |b|
        yield self.rgb(r,g,b)
      end
    end
  end
  range.size ** 3
end

.gpl(line) ⇒ Object

Creates a new RGB::Color from a line of gpl (Gimp color palette) input. Returns nil if not successful.

Examples:

RGB.gpl "255 153 204\tpink"


41
42
43
# File 'lib/redgreenblue/gpl.rb', line 41

def gpl(line)
  Color.gpl(line)
end

.gpl_header(name = nil, columns: nil) ⇒ Object

Returns a header for a .gpl file (Gimp color palette). Includes an optional name and number of columns.

Reverse-engineered from:

Examples:

RGB.gpl_header('Spring')


52
53
54
55
56
# File 'lib/redgreenblue/gpl.rb', line 52

def gpl_header(name=nil, columns: nil)
  "GIMP Palette\n" +
  ( name    ? "Name: #{name}\n"       : '' ) +
  ( columns ? "Columns: #{columns}\n" : '' )
end

.greenObject

Creates a pure green RGB::Color.



103
104
105
# File 'lib/redgreenblue/lazy.rb', line 103

def green
  Color.green
end

.grey(lightness = 0.5) ⇒ Object Also known as: gray

Creates a grey RGB::Color. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).

::gray is an alias for ::grey.



90
91
92
# File 'lib/redgreenblue/lazy.rb', line 90

def grey(lightness=0.5)
  Color.grey(lightness)
end

.hex(hex_string) ⇒ Object

Creates a new RGB::Color from a 6- or 3-digit hexadecimal string representing red, green, and blue.

The string may include a ‘#’ prefix.



52
53
54
# File 'lib/redgreenblue/hex.rb', line 52

def hex(hex_string)
  Color.hex(hex_string)
end

.hex_shorthand(hex_string) ⇒ Object

Returns a 3-digit shorthand version of a 6-digit hexadecimal string.

If a shorthand version is not possible, returns the original string.



59
60
61
# File 'lib/redgreenblue/hex.rb', line 59

def hex_shorthand(hex_string)
  hex_string.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' )
end

.hex_to_rgb(hex_string) ⇒ Object

Parses a 6- or 3-digit hexadecimal string. Returns three integers in the range 0..255, or nil if not successful.

The string may include a ‘#’ prefix.



67
68
69
70
71
72
73
74
75
# File 'lib/redgreenblue/hex.rb', line 67

def hex_to_rgb(hex_string)
  if hex_string =~ /^(#?)(\h\h)(\h\h)(\h\h)$/
    [$2, $3, $4].map { |h| h.to_i(16) }
  elsif hex_string =~ /^(#?)(\h)(\h)(\h)$/
    [$2, $3, $4].map { |h| (h*2).to_i(16) }
  else
    nil
  end
end

.hsbObject

Creates a new RGB::Color from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).



61
# File 'lib/redgreenblue/hsb.rb', line 61

alias hsb hsv

.hsl(*a) ⇒ Object

Creates a new RGB::Color from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).



96
97
98
# File 'lib/redgreenblue/hsl.rb', line 96

def hsl(*a)
  Color.hsl(*a)
end

.hsv(*a) ⇒ Object

Creates a new RGB::Color from HSV values: hue (0..360), saturation (0..1), and value (0..1).



96
97
98
# File 'lib/redgreenblue/hsv.rb', line 96

def hsv(*a)
  Color.hsv(*a)
end

.load_gpl(source = nil, file: nil, url: nil, compact: true, freeze: false) ⇒ Object

Loads a gpl (Gimp color palette) source and returns an array of RGB::Color objects.

Options:

  • file: Path to a .gpl file to be loaded.

  • url: URL for a .gpl source to be loaded.

  • compact: Defaults to true. If set to false, returns nil for each line that can not be parsed to an RGB color.

  • freeze: Defaults to false. If set to true, returns a frozen array of frozen objects.

Examples:

String

RGB.load_gpl "255 0 0\tred\n255 153 204\tpink\n"

File

RGB.load_gpl file: '/path/to/palette.gpl'
RGB.load_gpl file: '/path/to/palette.gpl', compact: false

URL

RGB.load_gpl url: 'https://lospec.com/palette-list/yuko-tomita-time.gpl'


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/redgreenblue/gpl.rb', line 73

def load_gpl(source=nil, file: nil, url: nil, compact: true, freeze: false)

  if ! source
    if file
      source = File.open file
    elsif url
      require 'open-uri'
      source = URI.open url
    end
  end

  if source.respond_to? :each_line
    list = source.each_line.map { |line| Color.gpl(line) }

    if compact
      list.compact!
    end

    if freeze
      list.freeze
      list.each &:freeze
    end

    list
  else
    raise ArgumentError, 'Not a valid source'
  end
end

.magentaObject

Creates a magenta RGB::Color.



123
124
125
# File 'lib/redgreenblue/lazy.rb', line 123

def magenta
  Color.magenta
end

.new(*a) ⇒ Object

Creates a new RGB::Color from red, green, and blue components as three values between 0 and 1.



91
92
93
# File 'lib/redgreenblue/base.rb', line 91

def new(*a)
  Color.new(*a)
end

.pick(default_color = new) ⇒ Object

On Mac OS, shows the color picker and creates an RGB::Color with the chosen color. Not available on other platforms.

If no default color is specified, the picker defaults to a middle grey.



75
76
77
# File 'lib/redgreenblue/os/mac.rb', line 75

def pick(default_color=new)
  Color.pick(default_color)
end

.randObject

Creates a new RGB::Color with random red, green, and blue values.



33
34
35
# File 'lib/redgreenblue/random.rb', line 33

def rand
  Color.rand
end

.redObject

Creates a pure red RGB::Color.



98
99
100
# File 'lib/redgreenblue/lazy.rb', line 98

def red
  Color.red
end

.rgb(*rgb) ⇒ Object

Creates a new RGB::Color from red, green, and blue components as integers in the range 0..255 (three 8-bit values).



73
74
75
# File 'lib/redgreenblue/24bit.rb', line 73

def rgb(*rgb)
  Color.rgb(*rgb)
end

.rgb565(rgb565_string) ⇒ Object

Creates a new RGB::Color from 16-bit RGB565 data.



33
34
35
# File 'lib/redgreenblue/rgb565.rb', line 33

def rgb565(rgb565_string)
  Color.rgb565(rgb565_string)
end

.rrggbb(*rrggbb) ⇒ Object

Creates a new RGB::Color from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).



62
63
64
# File 'lib/redgreenblue/48bit.rb', line 62

def rrggbb(*rrggbb)
  Color.rrggbb(*rrggbb)
end

.styleObject

Returns the current inspect style.



75
76
77
# File 'lib/redgreenblue/inspect.rb', line 75

def style
  @@style ||= base_style
end

.style=(s) ⇒ Object

Selects an inspect style.

Only the first few characters of your preferred style are required.



87
88
89
# File 'lib/redgreenblue/inspect.rb', line 87

def style=(s)
  @@style = styles.grep( /^#{s.to_s.downcase}/ ).first || style
end

.stylesObject

Returns a list of all available inspect styles.



80
81
82
# File 'lib/redgreenblue/inspect.rb', line 80

def styles
  ( Color.instance_methods + Color.private_instance_methods ).grep( /^_inspect_(.*)/ ) { $1 }.sort
end

.versionObject

Returns RGB::VERSION.



7
8
9
# File 'lib/redgreenblue/version.rb', line 7

def self.version
  VERSION
end

.whiteObject

Creates a white RGB::Color.



78
79
80
# File 'lib/redgreenblue/lazy.rb', line 78

def white
  Color.white
end

.yellowObject

Creates a yellow RGB::Color.



113
114
115
# File 'lib/redgreenblue/lazy.rb', line 113

def yellow
  Color.yellow
end