color_fun 1.0.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.
- data/README.md +66 -0
- data/color_fun.gemspec +17 -0
- data/lib/color_fun.rb +6 -0
- data/lib/color_fun/gradient.rb +51 -0
- data/lib/color_fun/hsl.rb +67 -0
- data/lib/color_fun/rgb.rb +62 -0
- metadata +50 -0
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Color Fun
|
2
|
+
|
3
|
+
This gem provides a few useful methods associated with colors for use in
|
4
|
+
web-based applications. This was all written for a little app we're working on.
|
5
|
+
|
6
|
+
## Example Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
# Initializing colors
|
10
|
+
red = ColorFun::RBG.new(255,0,0) # => #<ColorFun::RGB:0x007f9 @red=255, @green=0, @blue=0>
|
11
|
+
red = ColorFun::RBG.from_hex('ff0000') # => #<ColorFun::RGB:0x007f9 @red=255, @green=0, @blue=0>
|
12
|
+
|
13
|
+
# Access colors
|
14
|
+
red.red # => 255
|
15
|
+
red.green # => 0
|
16
|
+
red.blue # => 0
|
17
|
+
|
18
|
+
# Return perceived brightness of the colors
|
19
|
+
red.perceived_brightness # => 139.43627576782163
|
20
|
+
red.dark? # => true
|
21
|
+
red.light? # => false
|
22
|
+
|
23
|
+
# Return a lighter/darker version of the color. Accepts (optionally) a percentage.
|
24
|
+
red.lighter # => #<ColorFun::RGB:0x007fa @red=255, @green=127, @blue=127>
|
25
|
+
red.lighter(75) # => #<ColorFun::RGB:0x007fa @red=255, @green=191, @blue=191>
|
26
|
+
red.darker # => #<ColorFun::RGB:0x007fa @red=127, @green=0, @blue=0>
|
27
|
+
red.darker(25) # => #<ColorFun::RGB:0x007fa @red=191, @green=0, @blue=0>
|
28
|
+
|
29
|
+
# Return a hex value for the color
|
30
|
+
red.hex # => 'ff0000'
|
31
|
+
|
32
|
+
# Return an HSL object for the color
|
33
|
+
red.hsl # => #<ColorFun::HSL:0x007fa @hue=0.0, @saturation=100.0, @lightness=100.0>
|
34
|
+
red.hsl.hue # => 0.0
|
35
|
+
red.hsl.saturation # => 100.0
|
36
|
+
red.hsl.lightness # => 100.0
|
37
|
+
red.hsl.name # => 'Red'
|
38
|
+
|
39
|
+
# Quick access to key colors
|
40
|
+
red = ColorFun::RGB.red
|
41
|
+
green = ColorFun::RGB.green
|
42
|
+
blue = ColorFun::RGB.blue
|
43
|
+
```
|
44
|
+
|
45
|
+
## Gradients
|
46
|
+
|
47
|
+
Another cool little feature in here is the ability to work with gradients. This will automatically
|
48
|
+
create a linear gradient between colours you provide.
|
49
|
+
|
50
|
+
### Example usage with gradientds
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gradient = ColorFun::Gradient.new('e10025', '00a5e2', '7ae100')
|
54
|
+
|
55
|
+
# Optionally, set the resolution for the gradient (default is 100)
|
56
|
+
gradient.resolution = 100
|
57
|
+
|
58
|
+
# Get the colour of the gradient at a specific point
|
59
|
+
gradient.at(50) # => #<ColorFun::RGB:0x007fb @red=0, @green=165, @blue=226>
|
60
|
+
gradient.at(25) # => #<ColorFun::RGB:0x007fb @red=112, @green=82, @blue=131>
|
61
|
+
gradient.at(100) # => #<ColorFun::RGB:0x007fb @red=122, @green=225, @blue=0>
|
62
|
+
gradient.at(0) # => #<ColorFun::RGB:0x007fb @red=225, @green=0, @blue=37>
|
63
|
+
|
64
|
+
# Return all the points for each part of the gradient
|
65
|
+
gradient.points # => Array of all points as if you called the at method on each
|
66
|
+
```
|
data/color_fun.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
|
2
|
+
require 'color_fun'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'color_fun'
|
6
|
+
s.version = ColorFun::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.summary = "A little gem for playing with colors in Ruby"
|
9
|
+
s.description = s.summary
|
10
|
+
s.files = Dir["**/*"]
|
11
|
+
s.bindir = "bin"
|
12
|
+
s.require_path = 'lib'
|
13
|
+
s.has_rdoc = false
|
14
|
+
s.author = "Adam Cooke"
|
15
|
+
s.email = "adam@atechmedia.com"
|
16
|
+
s.homepage = "http://atechmedia.com"
|
17
|
+
end
|
data/lib/color_fun.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module ColorFun
|
2
|
+
class Gradient
|
3
|
+
|
4
|
+
attr_accessor :resolution
|
5
|
+
|
6
|
+
def initialize(*colors)
|
7
|
+
@colors = colors
|
8
|
+
@resolution = 100
|
9
|
+
end
|
10
|
+
|
11
|
+
def colors
|
12
|
+
@colors.map do |color|
|
13
|
+
color.scan(/../).map { |c| c.to_i(16) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def differences
|
18
|
+
colors[0..-2].each_with_index.map do |color, i|
|
19
|
+
color.dup.each_with_index.map { |c, j| colors[i + 1][j] - c }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def color_window_size
|
24
|
+
@resolution / differences.size
|
25
|
+
end
|
26
|
+
|
27
|
+
def points
|
28
|
+
@resolution.times.map { |i| self.at(i) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def at(point)
|
32
|
+
window = point / color_window_size
|
33
|
+
|
34
|
+
if point == @resolution
|
35
|
+
difference = differences.last
|
36
|
+
position = color_window_size
|
37
|
+
window = differences.size - 1
|
38
|
+
else
|
39
|
+
difference = differences[window]
|
40
|
+
position = point % color_window_size
|
41
|
+
end
|
42
|
+
|
43
|
+
new_colors = difference.each_with_index.map do |dc, i|
|
44
|
+
(colors[window][i] + (dc * (position.to_f / color_window_size.to_f))).to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
ColorFun::RGB.new(*new_colors)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module ColorFun
|
2
|
+
class HSL
|
3
|
+
|
4
|
+
attr_accessor :hue, :saturation, :lightness
|
5
|
+
|
6
|
+
def initialize(h, s, l)
|
7
|
+
@hue, @saturation, @lightness = h, s, l
|
8
|
+
end
|
9
|
+
|
10
|
+
# Return a name for this colour
|
11
|
+
def name
|
12
|
+
Array.new.tap do |s|
|
13
|
+
if @saturation == 0 && @lightness == 0
|
14
|
+
s << "Black"
|
15
|
+
elsif @saturation == 0 && @lightness == 100
|
16
|
+
s << "White"
|
17
|
+
elsif @saturation <= 15 && @saturation > 0
|
18
|
+
s << "Light"
|
19
|
+
elsif @lightness < 40 && @saturation > 0
|
20
|
+
s << "Dark"
|
21
|
+
end
|
22
|
+
|
23
|
+
if @saturation > 0
|
24
|
+
s << case @hue
|
25
|
+
when 0..8, 347..360 then 'Red'
|
26
|
+
when 9..42 then 'Orange'
|
27
|
+
when 43..61 then 'Yellow'
|
28
|
+
when 62..158 then 'Green'
|
29
|
+
when 159..264 then 'Blue'
|
30
|
+
when 265..285 then 'Purple'
|
31
|
+
when 286..346 then 'Pink'
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s << "Grey"
|
35
|
+
end
|
36
|
+
end.join(' ')
|
37
|
+
end
|
38
|
+
|
39
|
+
# Create a new HSL object based on the RGB values
|
40
|
+
def self.from_rbg(r,g,b)
|
41
|
+
colors = [r / 255.0, g / 255.0, b / 255.0]
|
42
|
+
max = colors.max
|
43
|
+
min = colors.min
|
44
|
+
d = max - min
|
45
|
+
v = max * 100
|
46
|
+
|
47
|
+
s = max != 0.0 ? d / max *100 : 0.0
|
48
|
+
|
49
|
+
if s == 0.0
|
50
|
+
h = 0.0
|
51
|
+
else
|
52
|
+
case max
|
53
|
+
when colors[0]
|
54
|
+
h = (colors[1] - colors[2]) / d
|
55
|
+
when colors[1]
|
56
|
+
h = 2 + (colors[2] - colors[0]) / d
|
57
|
+
when colors[2]
|
58
|
+
h = 4 + (colors[0] - colors[1]) / d
|
59
|
+
end
|
60
|
+
h *= 60.0
|
61
|
+
h += 360.0 if (h < 0)
|
62
|
+
end
|
63
|
+
self.new(h, s, v)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module ColorFun
|
2
|
+
class RGB
|
3
|
+
|
4
|
+
# Accessors for accessing the individual colors
|
5
|
+
attr_accessor :red, :green, :blue
|
6
|
+
|
7
|
+
# Initialize by passinga red, green and blue hex code
|
8
|
+
def initialize(red, green, blue)
|
9
|
+
@red, @green, @blue = red.to_i, green.to_i, blue.to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return the brightness value for a given colour (HSP)
|
13
|
+
def perceived_brightness
|
14
|
+
@brightness ||= Math.sqrt((0.299 * @red * @red) + (0.587 * @green * @green) + (0.114 * @blue * @blue))
|
15
|
+
end
|
16
|
+
|
17
|
+
# Return whether or not the color is visibly dark to the human eye?
|
18
|
+
def dark?
|
19
|
+
perceived_brightness < 160
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return whether or not the color is visibly light to the human eye?
|
23
|
+
def light?
|
24
|
+
perceived_brightness > 161
|
25
|
+
end
|
26
|
+
|
27
|
+
# Return a new color object for a lighter version of this color
|
28
|
+
def lighter(percent = 50)
|
29
|
+
percent = (percent / 100.0)
|
30
|
+
RGB.new((255 - @red) * percent + @red, (255 - @green) * percent + @green, (255 - @blue) * percent + @blue)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return a new color object for a darker version of this color
|
34
|
+
def darker(percent = 50)
|
35
|
+
percent = ((100 - percent) / 100.0)
|
36
|
+
RGB.new(@red * percent, @green * percent, @blue * percent)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return a hex string for this color
|
40
|
+
def hex
|
41
|
+
[@red, @green, @blue].map do |c|
|
42
|
+
c.to_s(16).rjust(2, '0')
|
43
|
+
end.join
|
44
|
+
end
|
45
|
+
|
46
|
+
# Return a HSL version of this color
|
47
|
+
def hsl
|
48
|
+
@hsl ||= HSL.from_rbg(@red, @green, @blue)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Initialize a new color object for a given hex code
|
52
|
+
def self.from_hex(hex)
|
53
|
+
self.new(*hex.scan(/../).map { |i| i.to_i(16) })
|
54
|
+
end
|
55
|
+
|
56
|
+
# Methods to create raw red, green and blue colors
|
57
|
+
def self.red; self.new(255,0,0); end
|
58
|
+
def self.green; self.new(0,255,0); end
|
59
|
+
def self.blue; self.new(0,0,255); end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color_fun
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A little gem for playing with colors in Ruby
|
15
|
+
email: adam@atechmedia.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- color_fun.gemspec
|
21
|
+
- lib/color_fun/gradient.rb
|
22
|
+
- lib/color_fun/hsl.rb
|
23
|
+
- lib/color_fun/rgb.rb
|
24
|
+
- lib/color_fun.rb
|
25
|
+
- README.md
|
26
|
+
homepage: http://atechmedia.com
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.23
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: A little gem for playing with colors in Ruby
|
50
|
+
test_files: []
|