motion-colortools 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzdjMDIxMWEwNWJhZjI2NDM3ZDQyYTFhYjZiNWYxZWVlMzFmYjQ3ZA==
5
+ data.tar.gz: !binary |-
6
+ YTEyZjJmNzRhZmMxYTMzMjk0YzVlMzY3OTk5MDZiNTdmMjUyNWUwNA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Y2QwMDQ5ODQwZTkzMjIzMzA2MmY5ZTg5NjdiNDdlODZlNzIzZjRkZjk2MDY5
10
+ ZGE3NjQyZmM4YzFmMDYzNDNlYWZjYmE5OTgyMTQzODJiNGNhZmVkM2U4M2U1
11
+ NGI1MTViZTk1OGExMjE2ZDM1MWFhZDJiMjAzN2I0MGI0MGU5ODU=
12
+ data.tar.gz: !binary |-
13
+ NDFlOGViYzFkODg5OGNjYWM5MzgwYTlhOWFhYzJiM2MxYWZlNDJjYzFmNTFk
14
+ MmQ1YjVjZjIxOGM2NmExODQwMjliOTg4N2NiNWFlYzFlZjFlY2U0N2IzNzBm
15
+ YjkwOGQxZDY4MjBhOWUxNDAxMmY5YTJlYWViODNhYWYwNTIzYmM=
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy
2
+ of this software and associated documentation files (the "Software"), to deal
3
+ in the Software without restriction, including without limitation the rights
4
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5
+ copies of the Software, and to permit persons to whom the Software is
6
+ furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in
9
+ all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17
+ THE SOFTWARE.
18
+
@@ -0,0 +1,108 @@
1
+ # motion-colortools
2
+
3
+ This gem provides some methods for manipulating UIColors in Rubymotion.
4
+
5
+ Examples:
6
+
7
+ `lighten` and `darken`:
8
+ ```ruby
9
+ test_color = UIColor.colorWithHue(0.5, saturation:0.5, brightness:0.5, alpha:1.0)
10
+
11
+ # Each of these methods takes one float between 0.0 and 1.0
12
+
13
+ # increase brightness by 0.2
14
+ test_color.lighten(0.2) # new brightness value of 0.7
15
+
16
+ # or darken it by 0.2
17
+ test_color.darken(0.2) # new brightness value of 0.3
18
+ ```
19
+
20
+
21
+
22
+
23
+ `scale_lighten` and `scale_darken`:
24
+ ```ruby
25
+
26
+ # scale_lighten and scale_darken each take a float between 0.0 and 1.0 as a percentage.
27
+ # scale_lighten lightens a color by 50% of the difference between the color's current brightness and 1.0, so:
28
+ test_color = UIColor.colorWithHue(0.5, saturation:0.5, brightness:0.8, alpha:1.0)
29
+ test_color.scale_lighten(0.5) # new brightness value of 0.9
30
+
31
+ #scale_darken does the inverse:
32
+ test_color = UIColor.colorWithHue(0.5, saturation:0.5, brightness:0.8, alpha:1.0)
33
+ test_color.scale_darken(0.5) # new brightness value of 0.4
34
+ ```
35
+
36
+
37
+
38
+
39
+ `saturate` and `desaturate`:
40
+ ```ruby
41
+ # saturate and desaturate work much line lighten and darken.
42
+
43
+ test_color = UIColor.colorWithHue(0.5, saturation:0.5, brightness:0.5, alpha:1.0)
44
+ test_color.saturate(0.3) # new saturation value of 0.8
45
+ test_color.desaturate(0.3) # new saturation value of 0.2
46
+ ```
47
+
48
+
49
+
50
+
51
+ `tint` and `shade`:
52
+ ```ruby
53
+ # rather than just adjusting brightness, tint and shade mix white and black into the
54
+ # color, respectively. They each take a float between 0.0 and 1.0, representing
55
+ # the percentage of white or black to mix in.
56
+ # tint decreases saturation while increasing brightness, shade does the inverse:
57
+
58
+ test_color = UIColor.colorWithHue(0.5, saturation:0.5, brightness:0.5, alpha:1.0)
59
+ test_color.tint(0.2)
60
+ # new values: hue=>0.5, :saturation=>0.35, :brightness=>0.69, :alpha=>1.0
61
+
62
+ # shade does the inverse:
63
+ test_color.shade(0.2)
64
+ # new values: hue=>0.5, :saturation=>0.83, :brightness=>0.29, :alpha=>1.0
65
+ ```
66
+
67
+
68
+
69
+
70
+ There are also some methods that make it easier to work with UIColors:
71
+ ```ruby
72
+ UIColor.blueColor.rgb # returns rgb values in a hash:
73
+ => {:red=>0.0, :green=>0.0, :blue=>1.0, :alpha=>1.0}
74
+
75
+ UIColor.blueColor.hsb # returns hsb values in a hash:
76
+ => {:hue=>0.666666507720947, :saturation=>1.0, :brightness=>1.0, :alpha=>1.0}
77
+
78
+ UIColor.blueColor.grayscale? # tests if a color is grayscale or not
79
+ => false
80
+ UIColor.whiteColor.grayscale?
81
+ => true
82
+
83
+ UIColor.whiteColor.grayscale # returns grayscale values in a hash
84
+ => {:white=>1.0, :alpha=>1.0}
85
+ ```
86
+
87
+
88
+ ## Installation
89
+
90
+ Add this line to your application's Gemfile:
91
+
92
+ gem 'motion-colortools'
93
+
94
+ And then execute:
95
+
96
+ $ bundle
97
+
98
+ Or install it yourself as:
99
+
100
+ $ gem install motion-colortools
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
6
+ Motion::Project::App.setup do |app|
7
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
8
+ end
@@ -0,0 +1,3 @@
1
+ module MotionColorTools
2
+ Version = "0.1.0"
3
+ end
@@ -0,0 +1,148 @@
1
+ class UIColor
2
+
3
+ def hsb
4
+ get_hsb
5
+ end
6
+
7
+ def rgb
8
+ get_rgb
9
+ end
10
+
11
+ def grayscale
12
+ get_grayscale
13
+ end
14
+
15
+ def lighten(amount)
16
+ if components == 4
17
+ color_values = hsb
18
+ new_brightness = color_values[:brightness] + amount
19
+ options = {brightness: new_brightness}
20
+ adjust_hsb(options)
21
+ else
22
+ self.tint(amount)
23
+ end
24
+ end
25
+
26
+ def scale_lighten(amount)
27
+ if components == 4
28
+ color_values = hsb
29
+ diff = 1.0 - color_values[:brightness]
30
+ new_brightness = color_values[:brightness] + (diff * amount)
31
+ options = {brightness: new_brightness}
32
+ adjust_hsb(options)
33
+ else
34
+ color_values = grayscale
35
+ diff = 1.0 - color_values[:white]
36
+ new_brightness = color_values[:white] + (diff * amount)
37
+ UIColor.colorWithWhite(new_brightness, alpha:(color_values[:alpha]))
38
+ end
39
+ end
40
+
41
+ def darken(amount)
42
+ if components == 4
43
+ color_values = hsb
44
+ new_brightness = color_values[:brightness] - amount
45
+ options = {brightness: new_brightness}
46
+ adjust_hsb(options)
47
+ else
48
+ self.shade(amount)
49
+ end
50
+ end
51
+
52
+ def scale_darken(amount)
53
+ if components == 4
54
+ color_values = hsb
55
+ new_brightness = color_values[:brightness] - (color_values[:brightness] * amount)
56
+ options = {brightness: new_brightness}
57
+ adjust_hsb(options)
58
+ else
59
+ color_values = grayscale
60
+ new_brightness = color_values[:white] - (color_values[:white] * amount)
61
+ UIColor.colorWithWhite(new_brightness, alpha:(color_values[:alpha]))
62
+ end
63
+ end
64
+
65
+ def desaturate(amount)
66
+ return self if components == 2
67
+ color_values = hsb
68
+ new_saturation = color_values[:saturation] - amount
69
+ options = {saturation: new_saturation}
70
+ adjust_hsb(options)
71
+ end
72
+
73
+ def saturate(amount)
74
+ return self if components == 2
75
+ color_values = hsb
76
+ new_saturation = color_values[:saturation] + amount
77
+ options = {saturation: new_saturation}
78
+ adjust_hsb(options)
79
+ end
80
+
81
+ def tint(amount)
82
+ if components == 4
83
+ color_values = rgb
84
+ UIColor.colorWithRed((color_values[:red] + amount), green:(color_values[:green] + amount), blue:(color_values[:blue] + amount), alpha:color_values[:alpha])
85
+ else
86
+ color_values = grayscale
87
+ UIColor.colorWithWhite((color_values[:white] + amount), alpha:(color_values[:alpha]))
88
+ end
89
+ end
90
+
91
+ def shade(amount)
92
+ if components == 4
93
+ color_values = rgb
94
+ UIColor.colorWithRed((color_values[:red] - amount), green:(color_values[:green] - amount), blue:(color_values[:blue] - amount), alpha:color_values[:alpha])
95
+ else
96
+ color_values = grayscale
97
+ UIColor.colorWithWhite((color_values[:white] - amount), alpha:(color_values[:alpha]))
98
+ end
99
+
100
+ end
101
+
102
+ def grayscale?
103
+ components == 2
104
+ end
105
+
106
+ def cg
107
+ self.CGColor
108
+ end
109
+
110
+
111
+ private
112
+
113
+ def components
114
+ CGColorGetNumberOfComponents(self.cg)
115
+ end
116
+
117
+ def get_grayscale
118
+ white = Pointer.new :float
119
+ alpha = Pointer.new :float
120
+ self.getWhite(white, alpha:alpha)
121
+ {white: white[0], alpha: alpha[0] }
122
+ end
123
+
124
+ def get_hsb
125
+ hue = Pointer.new :float
126
+ saturation = Pointer.new :float
127
+ brightness = Pointer.new :float
128
+ alpha = Pointer.new :float
129
+ self.getHue(hue, saturation:saturation, brightness:brightness, alpha:alpha)
130
+ { hue: hue[0], saturation: saturation[0], brightness: brightness[0], alpha: alpha[0] }
131
+ end
132
+
133
+ def get_rgb
134
+ red = Pointer.new :float
135
+ green = Pointer.new :float
136
+ blue = Pointer.new :float
137
+ alpha = Pointer.new :float
138
+ self.getRed(red, green:green, blue:blue, alpha:alpha)
139
+ { red: red[0], green: green[0], blue: blue[0], alpha: alpha[0] }
140
+ end
141
+
142
+ def adjust_hsb(options)
143
+ defaults = hsb
144
+ new_values = defaults.merge(options)
145
+ UIColor.colorWithHue(new_values[:hue], saturation:new_values[:saturation], brightness:(new_values[:brightness]), alpha:new_values[:alpha])
146
+ end
147
+
148
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-colortools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin McGladdery
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A set of methods for UIColor to darken, lighten, tint, shade, saturate
28
+ and desaturate existing colors.
29
+ email:
30
+ - kevin.mcgladdery@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/motion-colortools.rb
38
+ - lib/project/motion-colortools.rb
39
+ - lib/project/uicolor.rb
40
+ homepage: http://github.com/runkmc/motion-colortools
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A set of utility methods added to the UIColor class for manipulating colors.
63
+ test_files: []