Class: Colour

Inherits:
Object
  • Object
show all
Defined in:
lib/nswtopo/helpers/colour.rb

Constant Summary collapse

Error =
Class.new RuntimeError
COLOURS =
YAML.load "aliceblue: [240, 248, 255]\nantiquewhite: [250, 235, 215]\naqua: [0, 255, 255]\naquamarine: [127, 255, 212]\nazure: [240, 255, 255]\nbeige: [245, 245, 220]\nbisque: [255, 228, 196]\nblack: [0, 0, 0]\nblanchedalmond: [255, 235, 205]\nblue: [0, 0, 255]\nblueviolet: [138, 43, 226]\nbrown: [165, 42, 42]\nburlywood: [222, 184, 135]\ncadetblue: [95, 158, 160]\nchartreuse: [127, 255, 0]\nchocolate: [210, 105, 30]\ncoral: [255, 127, 80]\ncornflowerblue: [100, 149, 237]\ncornsilk: [255, 248, 220]\ncrimson: [220, 20, 60]\ncyan: [0, 255, 255]\ndarkblue: [0, 0, 139]\ndarkcyan: [0, 139, 139]\ndarkgoldenrod: [184, 134, 11]\ndarkgray: [169, 169, 169]\ndarkgreen: [0, 100, 0]\ndarkgrey: [169, 169, 169]\ndarkkhaki: [189, 183, 107]\ndarkmagenta: [139, 0, 139]\ndarkolivegreen: [85, 107, 47]\ndarkorange: [255, 140, 0]\ndarkorchid: [153, 50, 204]\ndarkred: [139, 0, 0]\ndarksalmon: [233, 150, 122]\ndarkseagreen: [143, 188, 143]\ndarkslateblue: [72, 61, 139]\ndarkslategray: [47, 79, 79]\ndarkslategrey: [47, 79, 79]\ndarkturquoise: [0, 206, 209]\ndarkviolet: [148, 0, 211]\ndeeppink: [255, 20, 147]\ndeepskyblue: [0, 191, 255]\ndimgray: [105, 105, 105]\ndimgrey: [105, 105, 105]\ndodgerblue: [30, 144, 255]\nfirebrick: [178, 34, 34]\nfloralwhite: [255, 250, 240]\nforestgreen: [34, 139, 34]\nfuchsia: [255, 0, 255]\ngainsboro: [220, 220, 220]\nghostwhite: [248, 248, 255]\ngold: [255, 215, 0]\ngoldenrod: [218, 165, 32]\ngray: [128, 128, 128]\ngrey: [128, 128, 128]\ngreen: [0, 128, 0]\ngreenyellow: [173, 255, 47]\nhoneydew: [240, 255, 240]\nhotpink: [255, 105, 180]\nindianred: [205, 92, 92]\nindigo: [75, 0, 130]\nivory: [255, 255, 240]\nkhaki: [240, 230, 140]\nlavender: [230, 230, 250]\nlavenderblush: [255, 240, 245]\nlawngreen: [124, 252, 0]\nlemonchiffon: [255, 250, 205]\nlightblue: [173, 216, 230]\nlightcoral: [240, 128, 128]\nlightcyan: [224, 255, 255]\nlightgoldenrodyellow: [250, 250, 210]\nlightgray: [211, 211, 211]\nlightgreen: [144, 238, 144]\nlightgrey: [211, 211, 211]\nlightpink: [255, 182, 193]\nlightsalmon: [255, 160, 122]\nlightseagreen: [32, 178, 170]\nlightskyblue: [135, 206, 250]\nlightslategray: [119, 136, 153]\nlightslategrey: [119, 136, 153]\nlightsteelblue: [176, 196, 222]\nlightyellow: [255, 255, 224]\nlime: [0, 255, 0]\nlimegreen: [50, 205, 50]\nlinen: [250, 240, 230]\nmagenta: [255, 0, 255]\nmaroon: [128, 0, 0]\nmediumaquamarine: [102, 205, 170]\nmediumblue: [0, 0, 205]\nmediumorchid: [186, 85, 211]\nmediumpurple: [147, 112, 219]\nmediumseagreen: [60, 179, 113]\nmediumslateblue: [123, 104, 238]\nmediumspringgreen: [0, 250, 154]\nmediumturquoise: [72, 209, 204]\nmediumvioletred: [199, 21, 133]\nmidnightblue: [25, 25, 112]\nmintcream: [245, 255, 250]\nmistyrose: [255, 228, 225]\nmoccasin: [255, 228, 181]\nnavajowhite: [255, 222, 173]\nnavy: [0, 0, 128]\noldlace: [253, 245, 230]\nolive: [128, 128, 0]\nolivedrab: [107, 142, 35]\norange: [255, 165, 0]\norangered: [255, 69, 0]\norchid: [218, 112, 214]\npalegoldenrod: [238, 232, 170]\npalegreen: [152, 251, 152]\npaleturquoise: [175, 238, 238]\npalevioletred: [219, 112, 147]\npapayawhip: [255, 239, 213]\npeachpuff: [255, 218, 185]\nperu: [205, 133, 63]\npink: [255, 192, 203]\nplum: [221, 160, 221]\npowderblue: [176, 224, 230]\npurple: [128, 0, 128]\nred: [255, 0, 0]\nrosybrown: [188, 143, 143]\nroyalblue: [65, 105, 225]\nsaddlebrown: [139, 69, 19]\nsalmon: [250, 128, 114]\nsandybrown: [244, 164, 96]\nseagreen: [46, 139, 87]\nseashell: [255, 245, 238]\nsienna: [160, 82, 45]\nsilver: [192, 192, 192]\nskyblue: [135, 206, 235]\nslateblue: [106, 90, 205]\nslategray: [112, 128, 144]\nslategrey: [112, 128, 144]\nsnow: [255, 250, 250]\nspringgreen: [0, 255, 127]\nsteelblue: [70, 130, 180]\ntan: [210, 180, 140]\nteal: [0, 128, 128]\nthistle: [216, 191, 216]\ntomato: [255, 99, 71]\nturquoise: [64, 224, 208]\nviolet: [238, 130, 238]\nwheat: [245, 222, 179]\nwhite: [255, 255, 255]\nwhitesmoke: [245, 245, 245]\nyellow: [255, 255, 0]\nyellowgreen: [154, 205, 50]\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_or_array) ⇒ Colour

Returns a new instance of Colour.

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nswtopo/helpers/colour.rb', line 154

def initialize(string_or_array)
  @triplet = case string_or_array
  when Array then string_or_array.take(3).map(&:round)
  when *COLOURS.keys
    @name = string_or_array
    COLOURS[string_or_array]
  when /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i
    [$1, $2, $3].map { |hex| Integer("0x#{hex}") }
  when /^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/
    [$1, $2, $3].map(&:to_i)
  end
  raise Error, "invalid colour: #{string_or_array}" unless @triplet&.all?(0..255)
end

Instance Attribute Details

#tripletObject (readonly)

Returns the value of attribute triplet.



167
168
169
# File 'lib/nswtopo/helpers/colour.rb', line 167

def triplet
  @triplet
end

Instance Method Details

#mix(other, fraction) ⇒ Object



169
170
171
# File 'lib/nswtopo/helpers/colour.rb', line 169

def mix(other, fraction)
  Colour.new [triplet, other.triplet].along(fraction.to_f).map(&:to_i)
end

#to_sObject



173
174
175
# File 'lib/nswtopo/helpers/colour.rb', line 173

def to_s
  @name || "#%.2X%.2X%.2X" % triplet
end