Class: UIColor
- Defined in:
- lib/sugarcube/uicolor.rb,
lib/sugarcube/to_s/uicolor.rb
Instance Method Summary collapse
-
#+(color) ⇒ Object
blends two colors by averaging the RGB and alpha components.
- #alpha ⇒ Object
- #blue ⇒ Object
- #cgcolor ⇒ Object
-
#css_name ⇒ Object
returns the closest css name.
- #green ⇒ Object
- #hex ⇒ Object
- #inspect ⇒ Object
-
#invert ⇒ Object
inverts the RGB channel.
-
#mix_with(color, amount) ⇒ Object
a more generic color mixing method.
- #red ⇒ Object
- #system_name ⇒ Object
-
#to_a ⇒ Object
returns the components as an array of 32 bit RGB values.
-
#to_i ⇒ Object
returns the components OR'd together, as 32 bit RGB integer.
- #to_s ⇒ Object
- #uicolor(alpha = nil) ⇒ Object
Instance Method Details
#+(color) ⇒ Object
blends two colors by averaging the RGB and alpha components.
18 19 20 |
# File 'lib/sugarcube/uicolor.rb', line 18 def +(color) mix_with(color, 0.5) end |
#alpha ⇒ Object
71 72 73 |
# File 'lib/sugarcube/uicolor.rb', line 71 def alpha _sugarcube_colors && _sugarcube_colors[:alpha] end |
#blue ⇒ Object
67 68 69 |
# File 'lib/sugarcube/uicolor.rb', line 67 def blue _sugarcube_colors && _sugarcube_colors[:blue] end |
#cgcolor ⇒ Object
11 12 13 |
# File 'lib/sugarcube/uicolor.rb', line 11 def cgcolor self.CGColor end |
#css_name ⇒ Object
returns the closest css name
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sugarcube/uicolor.rb', line 109 def css_name my_color = self.to_i css_name = nil Symbol.css_colors.each_pair do |color, hex| if hex == my_color css_name = color break end end return css_name end |
#green ⇒ Object
63 64 65 |
# File 'lib/sugarcube/uicolor.rb', line 63 def green _sugarcube_colors && _sugarcube_colors[:green] end |
#hex ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/sugarcube/uicolor.rb', line 99 def hex my_color = self.to_i if my_color return '#' + my_color.to_s(16).rjust(6, '0') else nil end end |
#inspect ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sugarcube/to_s/uicolor.rb', line 19 def inspect alpha_s = ((alpha || 1) < 1 ? "(#{alpha})" : '') if system_name return "UIColor.#{system_name}#{alpha_s}" elsif hex return "'#{hex}'.uicolor#{alpha_s}" elsif css_name return ":#{css_name}.uicolor#{alpha_s}" else super end end |
#invert ⇒ Object
inverts the RGB channel. keeps the alpha channel unchanged
51 52 53 54 55 56 57 |
# File 'lib/sugarcube/uicolor.rb', line 51 def invert r = 1.0 - self.red g = 1.0 - self.green b = 1.0 - self.blue a = self.alpha UIColor.colorWithRed(r, green:g, blue:b, alpha:a) end |
#mix_with(color, amount) ⇒ Object
a more generic color mixing method. mixes two colors, but a second parameter determines how much of each. 0.5 means equal parts, 0.0 means use all of the first, and 1.0 means use all of the second
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sugarcube/uicolor.rb', line 25 def mix_with(color, amount) # make amount between 0 and 1 amount = [[0, amount].max, 1].min # start with precise amounts: 0, 0.5, and 1. if amount == 0 self elsif amount == 1 color elsif amount == 0.5 r = (self.red + color.red) / 2 g = (self.green + color.green) / 2 b = (self.blue + color.blue) / 2 a = (self.alpha + color.alpha) / 2 UIColor.colorWithRed(r, green:g, blue:b, alpha:a) else r = (color.red - self.red) * amount + self.red g = (color.green - self.green) * amount + self.green b = (color.blue - self.blue) * amount + self.blue a = (color.alpha - self.alpha) * amount + self.alpha UIColor.colorWithRed(r, green:g, blue:b, alpha:a) end end |
#red ⇒ Object
59 60 61 |
# File 'lib/sugarcube/uicolor.rb', line 59 def red _sugarcube_colors && _sugarcube_colors[:red] end |
#system_name ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sugarcube/uicolor.rb', line 121 def system_name system_color = nil Symbol.uicolors.each_pair do |color, method| if UIColor.send(method) == self system_color = method break end end return system_color.to_s end |
#to_a ⇒ Object
returns the components as an array of 32 bit RGB values
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sugarcube/uicolor.rb', line 88 def to_a if self.red && self.green && self.blue red = (self.red * 255).round green = (self.green * 255).round blue = (self.blue * 255).round return [red, green, blue] else return nil end end |
#to_i ⇒ Object
returns the components OR'd together, as 32 bit RGB integer.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sugarcube/uicolor.rb', line 76 def to_i if self.red && self.green && self.blue red = (self.red * 255).round << 16 green = (self.green * 255).round << 8 blue = (self.blue * 255).round return red + green + blue else return nil end end |
#to_s ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcube/to_s/uicolor.rb', line 3 def to_s system_color = system_name return system_color if system_color inside = self.css_name || self.hex if inside if self.alpha < 1 return "UIColor.color(#{inside}, #{alpha})" else return "UIColor.color(#{inside})" end else super end end |
#uicolor(alpha = nil) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/sugarcube/uicolor.rb', line 3 def uicolor(alpha=nil) if alpha self.colorWithAlphaComponent(alpha.to_f) else self end end |