| Class | RS::Graphics::RSColor |
| In: |
rsil/graphics/rscolor.rb
|
| Parent: | RS::Utils::RSObjcWrapper |
| Class: | RSColor |
| File: | rscolor.rb |
| Purpose: | RSColor represents a color through wrapping of RubyCocoas OSX::CIColor. You can access named colors by calling RSColor#color_<name> - the 16 VGA colors and the 216 HTML colors are included. RSColors are used with RS::Graphics::RSFilter. |
| Created by: | Mario Pehle, 2006/04/29 |
| Required modules: | - |
| Offers functions: | - |
| DEFAULT_ALPHA | = | 1.0 | When creating a new RSColor object, DEFAULT_ALPHA will be set for alpha component of the color if no alpha value is specified. | |
| COLORNAMES | = | { :black => '#000000', :maroon => '#800000', :green => '#008000', :olive => '#808000', :navy => '#000080', :purple => '#800080', :teal => '#008080', :gray => '#808080', :silver => '#C0C0C0', :red => '#FF0000', :lime => '#00FF00', :yellow => '#FFFF00', :blue => '#0000FF', :fuchsia => '#FF00FF', :aqua => '#00FFFF', :white => '#FFFFFF', :aliceblue => '#F0F8FF', :antiquewhite => '#FAEBD7', :aquamarine => '#7FFFD4', :azure => '#F0FFFF', :beige => '#F5F5DC', :blueviolet => '#8A2BE2', :brown => '#A52A2A', :burlywood => '#DEB887', :cadetblue => '#5F9EA0', :chartreuse => '#7FFF00', :chocolate => '#D2691E', :coral => '#FF7F50', :cornflowerblue => '#6495ED', :cornsilk => '#FFF8DC', :crimson => '#DC143C', :darkblue => '#00008B', :darkcyan => '#008B8B', :darkgoldenrod => '#B8860B', :darkgray => '#A9A9A9', :darkgreen => '#006400', :darkkhaki => '#BDB76B', :darkmagenta => '#8B008B', :darkolivegreen => '#556B2F', :darkorange => '#FF8C00', :darkorchid => '#9932CC', :darkred => '#8B0000', :darksalmon => '#E9967A', :darkseagreen => '#8FBC8F', :darkslateblue => '#483D8B', :darkslategray => '#2F4F4F', :darkturquoise => '#00CED1', :darkviolet => '#9400D3', :deeppink => '#FF1493', :deepskyblue => '#00BFFF', :dimgray => '#696969', :dodgerblue => '#1E90FF', :firebrick => '#B22222', :floralwhite => '#FFFAF0', :forestgreen => '#228B22', :gainsboro => '#DCDCDC', :ghostwhite => '#F8F8FF', :gold => '#FFD700', :goldenrod => '#DAA520', :greenyellow => '#ADFF2F', :honeydew => '#F0FFF0', :hotpink => '#FF69B4', :indianred => '#CD5C5C', :indigo => '#4B0082', :ivory => '#FFFFF0', :khaki => '#F0E68C', :lavender => '#E6E6FA', :lavenderblush => '#FFF0F5', :lawngreen => '#7CFC00', :lemonchiffon => '#FFFACD', :lightblue => '#ADD8E6', :lightcoral => '#F08080', :lightcyan => '#E0FFFF', :lightgoldenrodyellow => '#FAFAD2', :lightgreen => '#90EE90', :lightgrey => '#D3D3D3', :lightpink => '#FFB6C1', :lightsalmon => '#FFA07A', :lightseagreen => '#20B2AA', :lightskyblue => '#87CEFA', :lightslategray => '#778899', :lightsteelblue => '#B0C4DE', :lightyellow => '#FFFFE0', :limegreen => '#32CD32', :linen => '#FAF0E6', :mediumaquamarine => '#66CDAA', :mediumblue => '#0000CD', :mediumorchid => '#BA55D3', :mediumpurple => '#9370DB', :mediumseagreen => '#3CB371', :mediumslateblue => '#7B68EE', :mediumspringgreen => '#00FA9A', :mediumturquoise => '#48D1CC', :mediumvioletred => '#C71585', :midnightblue => '#191970', :mintcream => '#F5FFFA', :mistyrose => '#FFE4E1', :moccasin => '#FFE4B5', :navajowhite => '#FFDEAD', :oldlace => '#FDF5E6', :olivedrab => '#6B8E23', :orange => '#FFA500', :orangered => '#FF4500', :orchid => '#DA70D6', :palegoldenrod => '#EEE8AA', :palegreen => '#98FB98', :paleturquoise => '#AFEEEE', :palevioletred => '#DB7093', :papayawhip => '#FFEFD5', :peachpuff => '#FFDAB9', :peru => '#CD853F', :pink => '#FFC0CB', :plum => '#DDA0DD', :powderblue => '#B0E0E6', :rosybrown => '#BC8F8F', :royalblue => '#4169E1', :saddlebrown => '#8B4513', :salmon => '#FA8072', :sandybrown => '#F4A460', :seagreen => '#2E8B57', :seashell => '#FFF5EE', :sienna => '#A0522D', :skyblue => '#87CEEB', :slateblue => '#6A5ACD', :slategray => '#708090', :snow => '#FFFAFA', :springgreen => '#00FF7F', :steelblue => '#4682B4', :tan => '#D2B48C', :thistle => '#D8BFD8', :tomato => '#FF6347', :turquoise => '#40E0D0', :violet => '#EE82EE', :wheat => '#F5DEB3', :whitesmoke => '#F5F5F5', :yellowgreen => '#9ACD32' } | The hash COLORNAMES provides will known color values: The 16 basic colors and the 216 named web colors. You can get a RSColor object for these colors by calling RSColor#color_<key>. |
| Description: | Creates a RSColor for the given color name. All colors from RSColor#COLORNAMES will generate a valid color. If you know the color name, you also can call RSImage#color_<name>. |
| Precondition: | ‘name’ is a String or Symbol out of the keys of RSColor#COLORNAMES. |
| Postcondition: | - |
| Exceptions: | ArgumentError |
| Uses: | RSColor#color_for |
| Returns: | RSColor if valid arguments or nil if not |
| Parameters: | Name | i/o/io | default | Meaning |
| : | name | i | nil | String, the color name |
# File rsil/graphics/rscolor.rb, line 294
294: def self::color name
295: if name.respond_to? :to_str
296: return self::color_for(COLORNAMES[name.to_str.intern])
297: else
298: raise ArgumentError, 'Can not convert argument.'
299: end
300: nil
301: end
| Description: | Creates a RSColor for the given String. The String may have the following formats: Hexadecimal: xxxxxxxx, xxxxxx, xxxx, xxx; Float: x.x x.x x.x, x.x x.x x.x x.x; Integer: xxx xxx xxx, xxx xxx xxx xxx. |
| Precondition: | The String consists of 3 to 4 Elements. |
| Postcondition: | - |
| Exceptions: | ArgumentError |
| Uses: | String#chomp, String#strip, String#split, String#delete, RSColor#hex_to_float |
| Returns: | RSColor if valid arguments or nil if not |
| Parameters: | Name | i/o/io | default | Meaning |
| : | string | i | - | String, the string representing the color |
# File rsil/graphics/rscolor.rb, line 238
238: def self::color_for string
239: (rgba = string.to_str.chomp.strip.split) rescue raise ArgumentError, "Can not convert argument."
240: red = 0.0
241: green = 0.0
242: blue = 0.0
243: alpha = DEFAULT_ALPHA
244: if rgba.length == 1
245: hex = rgba.first.delete '#'
246: case hex.length
247: when 6, 8
248: red = self.hex_to_float hex[0, 2]
249: green = self.hex_to_float hex[2, 2]
250: blue = self.hex_to_float hex[4, 2]
251: alpha = self.hex_to_float hex[6, 2] if hex.length == 8
252: when 3, 4
253: red = self.hex_to_float hex[0, 1] * 2
254: green = self.hex_to_float hex[1, 1] * 2
255: blue = self.hex_to_float hex[2, 1] * 2
256: alpha = self.hex_to_float hex[3, 1] * 2 if hex.length == 4
257: else
258: raise ArgumentError,
259: 'Argument is no valid hexadecimal value for a color.'
260: end
261: elsif rgba.length >= 3 and rgba.length <= 4
262: begin
263: red = self.convert_to_valid rgba[0]
264: green = self.convert_to_valid rgba[1]
265: blue = self.convert_to_valid rgba[2]
266: alpha = self.convert_to_valid rgba[3] if rgba.length == 4
267: rescue
268: raise ArgumentError, 'Can not convert String to color.'
269: end
270: else
271: raise ArgumentError,
272: "Argument needs to have 1, 3 or 4 parts, but has #{rgba.length}."
273: end
274: assert 'red is not a float.' unless red.class == Float
275: assert 'green is not a float.' unless green.class == Float
276: assert 'blue is not a float.' unless blue.class == Float
277: assert 'alpha is not a float.' unless alpha.class == Float
278: self.new red, green, blue, alpha
279: end
| Description: | Converts a given String to a Float when possible. Maximum value is 1.0. If String represents a value larger 1.0 it will become 1.0. |
| Precondition: | The String consists of 3 to 4 Elements. |
| Postcondition: | - |
| Exceptions: | ArgumentError |
| Uses: | String#include?, Float, Integer |
| Returns: | RSColor if valid arguments or nil if not |
| Parameters: | Name | i/o/io | default | Meaning |
| : | string | i | - | String, the value to convert |
# File rsil/graphics/rscolor.rb, line 216
216: def self::convert_to_valid string
217: is_a_float = string.include? '.'
218: v = is_a_float ? Float(string) : Integer(string)
219: v = v.to_f / 256.0 unless is_a_float
220: v = 1.0 if v > 1.0
221: raise ArgumentError, 'Negative values not allowed.' if v < 0.0
222: v
223: end
| Description: | Converts a hexadecimal string to a Float. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | ArgumentError |
| Uses: | String#hex |
| Returns: | Float |
| Parameters: | Name | i/o/io | default | Meaning |
| : | string | i | - | String, the value to convert. |
# File rsil/graphics/rscolor.rb, line 196
196: def self::hex_to_float string
197: raise ArgumentError unless string.respond_to?(:hex)
198: r = string.hex/256.0
199: assert 'Return value has to be a Float.' unless r.kind_of? Float
200: r
201: end
| Description: | Creates a RSColor for the given values. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | ArgumentError |
| Uses: | OSX::CIColor.colorWithRed:green:blue:alpha |
| Returns: | RSColor |
| Parameters: | Name | i/o/io | default | Meaning |
| : | red | i | - | Float, the red value |
| : | green | i | - | Float, the green value |
| : | blue | i | - | Float, the blue value |
| : | alpha | i | RSColor#DEFAULT_ALPHA | Float, the alpha value |
# File rsil/graphics/rscolor.rb, line 319
319: def initialize red, green, blue, alpha=DEFAULT_ALPHA
320: unless red.respond_to?(:to_f) or green.respond_to?(:to_f) or
321: blue.respond_to?(:to_f) or alpha.respond_to?(:to_f)
322: raise "Arguments have to be of type Float or have to support 'to_f'."
323: end
324: @cicolor = OSX::CIColor.colorWithRed(
325: red.to_f,
326: :green, green.to_f,
327: :blue , blue.to_f,
328: :alpha, alpha.to_f
329: )
330: assert "@cicolor not initialized" if @cicolor.nil?
331: self
332: end
| Description: | Determines the alpha value of the color. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | OSX::CIColor#alpha |
| Returns: | Float |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/graphics/rscolor.rb, line 502
502: def alpha
503: @cicolor.alpha
504: end
| Description: | Sets the green value of the color. |
| Precondition: | - |
| Postcondition: | Value for alpha is changed. Creates a new OSX::CIColor because it is immutable. |
| Exceptions: | - |
| Uses: | - |
| Returns: | value if valid value, else nil |
| Parameters: | Name | i/o/io | default | Meaning |
| : | value | i | - | Float, the value to set |
# File rsil/graphics/rscolor.rb, line 428
428: def alpha= value
429: if value.kind_of? Float
430: @cicolor = OSX::CIColor.colorWithRed(
431: @cicolor.red,
432: :green, @cicolor.green,
433: :blue, @cicolor.blue,
434: :alpha, value.to_f
435: )
436: assert "@cicolor not reinitialized" if @cicolor.nil?
437: return value
438: end
439: nil
440: end
| Description: | Determines the blue value of the color. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | OSX::CIColor#blue |
| Returns: | Float |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/graphics/rscolor.rb, line 486
486: def blue
487: @cicolor.blue
488: end
| Description: | Sets the green value of the color. |
| Precondition: | - |
| Postcondition: | Value for blue is changed. Creates a new OSX::CIColor because it is immutable. |
| Exceptions: | - |
| Uses: | - |
| Returns: | value if valid value, else nil |
| Parameters: | Name | i/o/io | default | Meaning |
| : | value | i | - | Float, the value to set |
# File rsil/graphics/rscolor.rb, line 401
401: def blue= value
402: if value.kind_of? Float
403: @cicolor = OSX::CIColor.colorWithRed(
404: @cicolor.red,
405: :green, @cicolor.green,
406: :blue, value.to_f,
407: :alpha, @cicolor.alpha
408: )
409: assert "@cicolor not reinitialized" if @cicolor.nil?
410: return value
411: end
412: nil
413: end
| Description: | Determines the red value of the color. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | OSX::CIColor#green |
| Returns: | Float |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/graphics/rscolor.rb, line 470
470: def green
471: @cicolor.green
472: end
| Description: | Sets the green value of the color. |
| Precondition: | - |
| Postcondition: | Value for green is changed. Creates a new OSX::CIColor because it is immutable. |
| Exceptions: | - |
| Uses: | - |
| Returns: | value if valid value, else nil |
| Parameters: | Name | i/o/io | default | Meaning |
| : | value | i | - | Float, the value to set |
# File rsil/graphics/rscolor.rb, line 374
374: def green= value
375: if value.kind_of? Float
376: @cicolor = OSX::CIColor.colorWithRed(
377: @cicolor.red,
378: :green, value.to_f,
379: :blue, @cicolor.blue,
380: :alpha, @cicolor.alpha
381: )
382: assert "@cicolor not reinitialized" if @cicolor.nil?
383: return value
384: end
385: nil
386: end
| Description: | Gives access to the basic Core Image object of this object, a OSX::CIColor. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | - |
| Returns: | OSX::CIColor / OSX::OCObject |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/graphics/rscolor.rb, line 518
518: def objc_object
519: @cicolor
520: end
| Description: | Determines the red value of the color. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | OSX::CIColor#red |
| Returns: | Float |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/graphics/rscolor.rb, line 454
454: def red
455: @cicolor.red
456: end
| Description: | Sets the red value of the color. |
| Precondition: | - |
| Postcondition: | Value for red is changed. Creates a new OSX::CIColor because it is immutable. |
| Exceptions: | - |
| Uses: | - |
| Returns: | value if valid value, else nil |
| Parameters: | Name | i/o/io | default | Meaning |
| : | value | i | - | Float, the value to set |
# File rsil/graphics/rscolor.rb, line 347
347: def red= value
348: if value.kind_of? Float
349: @cicolor = OSX::CIColor.colorWithRed(
350: value.to_f,
351: :green, @cicolor.green,
352: :blue, @cicolor.blue,
353: :alpha, @cicolor.alpha
354: )
355: assert "@cicolor not reinitialized" if @cicolor.nil?
356: return value
357: end
358: nil
359: end