rgb 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.
Files changed (4) hide show
  1. data/README.md +54 -0
  2. data/lib/rgb.rb +1 -0
  3. data/lib/rgb/color.rb +146 -0
  4. metadata +81 -0
@@ -0,0 +1,54 @@
1
+ rgb
2
+ ===
3
+
4
+ A simple Ruby library built to handle the easy conversion and manipulation of colors. Inspired by compass-colors https://github.com/chriseppstein/compass-colors and jColour.js https://github.com/lingo/jcolour.
5
+
6
+ Example
7
+ ===
8
+
9
+ require "rgb"
10
+
11
+ # Supported input data color formas:
12
+ color = RGB::Color.from_rgb_hex("#333333")
13
+ color = RGB::Color.from_rgb_hex(0xFF0000)
14
+ color = RGB::Color.from_rgb(115, 38, 38)
15
+ color = RGB::Color.from_fractions(0, 1.0, 0.5) # HSL
16
+
17
+ # Supported color manipulations:
18
+ color.darken(20)
19
+ color.darken_percent(10)
20
+ color.darken!(20)
21
+ color.darken_percent!(10)
22
+ color.lighten(20)
23
+ color.lighten_percent(20)
24
+ color.lighten!(20)
25
+ color.lighten_percent!(20)
26
+ color.saturate(20)
27
+ color.saturate_percent(20)
28
+ color.saturate!(20)
29
+ color.saturate_percent!(20)
30
+ color.desaturate(20)
31
+ color.desaturate_percent(20)
32
+ color.desaturate!(20)
33
+ color.desaturate_percent!(20)
34
+
35
+ # Also you can adjust color hue, saturation, and lightness values manually:
36
+ color.h = 0.1
37
+ color.s = 0.2
38
+ color.l = 0.3
39
+
40
+ # Supported output formats:
41
+ color.to_rgb_hex
42
+ => "#732626"
43
+ color.to_hsl
44
+ => [0, 1.0, 0.5]
45
+ color.to_rgb
46
+ => [115, 38, 38]
47
+
48
+
49
+ Resources
50
+ ===
51
+
52
+ * GitHub Source: https://github.com/plashchynski/rgb
53
+
54
+ Copyright (c) 2013 Dmitry Plashchynski. Released under the MIT open source license.
@@ -0,0 +1 @@
1
+ require "rgb/color"
@@ -0,0 +1,146 @@
1
+ module RGB
2
+ class Color
3
+ attr_reader :h, :s, :l
4
+
5
+ def self.from_rgb_hex(color)
6
+ color = "#%.6x" % color if color.is_a? Integer
7
+ rgb = color[1,7].scan(/.{2}/).map{|component| component.to_i(16)}
8
+ from_rgb(*rgb)
9
+ end
10
+
11
+ def self.from_rgb(*rgb)
12
+ rgb.map!{|c| c / 255.0}
13
+ min_rgb = rgb.min
14
+ max_rgb = rgb.max
15
+ delta = max_rgb - min_rgb
16
+
17
+ lightness = (max_rgb + min_rgb) / 2.0
18
+
19
+ if delta < 1e-5
20
+ hue = 0
21
+ saturation = 0
22
+ else
23
+ saturation = if ( lightness < 0.5 )
24
+ delta / ( max_rgb + min_rgb )
25
+ else
26
+ delta / ( 2 - max_rgb - min_rgb )
27
+ end
28
+
29
+ deltas = rgb.map{|c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta}
30
+
31
+ hue = if (rgb[0] - max_rgb).abs < 1e-5
32
+ deltas[2] - deltas[1]
33
+ elsif (rgb[1] - max_rgb).abs < 1e-5
34
+ ( 1.0 / 3.0 ) + deltas[0] - deltas[2]
35
+ else
36
+ ( 2.0 / 3.0 ) + deltas[1] - deltas[0]
37
+ end
38
+ hue += 1 if hue < 0
39
+ hue -= 1 if hue > 1
40
+ end
41
+ from_fractions(hue, saturation, lightness)
42
+ end
43
+
44
+ def self.from_fractions(hue, saturation, lightness)
45
+ new(360 * hue, saturation, lightness)
46
+ end
47
+
48
+ def initialize(*hsl)
49
+ self.h, self.s, self.l = hsl
50
+ end
51
+
52
+ def to_rgb
53
+ m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s
54
+ m1 = l * 2 - m2
55
+ [hue_to_rgb(m1, m2, hp + 1.0/3), hue_to_rgb(m1, m2, hp), hue_to_rgb(m1, m2, hp - 1.0/3)].map { |c| (c * 0xff).round }
56
+ end
57
+
58
+ def to_hsl
59
+ [h,s,l]
60
+ end
61
+
62
+ def to_rgb_hex
63
+ "#" + to_rgb.map {|c| "%02X" % c }.join
64
+ end
65
+
66
+ def h=(hue)
67
+ @h = hue % 360
68
+ end
69
+
70
+ def s=(saturation)
71
+ @s = if saturation < 0
72
+ 0.0
73
+ elsif saturation > 1
74
+ 1.0
75
+ else
76
+ saturation
77
+ end
78
+ end
79
+
80
+ def l=(lightness)
81
+ @l = if lightness < 0
82
+ 0.0
83
+ elsif lightness > 1
84
+ 1.0
85
+ else
86
+ lightness
87
+ end
88
+ end
89
+
90
+ def lighten!(amount)
91
+ @l += amount / 100.0
92
+ end
93
+
94
+ def lighten_percent!(percentage)
95
+ @l += (1 - @l) * (percentage / 100.0)
96
+ end
97
+
98
+ def darken!(amount)
99
+ @l -= (amount / 100.0)
100
+ end
101
+
102
+ def darken_percent!(percentage)
103
+ @l *= 1.0 - (percentage / 100.0)
104
+ end
105
+
106
+ def saturate!(amount)
107
+ @s += amount / 100.0
108
+ end
109
+
110
+ def saturate_percent!(percentage)
111
+ @s += (1 - @s) * (percentage / 100.0)
112
+ end
113
+
114
+ def desaturate!(amount)
115
+ @s -= amount / 100.0
116
+ end
117
+
118
+ def desaturate_percent!(percentage)
119
+ @s *= (1.0 - (percentage / 100.0))
120
+ end
121
+
122
+ # define non-bang methods
123
+ [:darken, :darken_percent, :lighten, :lighten_percent, :saturate, :saturate_percent, :desaturate,
124
+ :desaturate_percent].each do |method_name|
125
+ define_method method_name do |*args|
126
+ dup.tap { |color| color.send(:"#{method_name}!", *args) }
127
+ end
128
+ end
129
+
130
+ private
131
+ #hue as a percentage
132
+ def hp
133
+ h / 360.0
134
+ end
135
+
136
+ # helper for making rgb
137
+ def hue_to_rgb(m1, m2, h)
138
+ h += 1 if h < 0
139
+ h -= 1 if h > 1
140
+ return m1 + (m2 - m1) * h * 6 if h * 6 < 1
141
+ return m2 if h * 2 < 1
142
+ return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
143
+ return m1
144
+ end
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rgb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitry Plashchynski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A library built to handle the easy conversion, comparison and manipulation
47
+ of colors with CSS-style hex color notation.
48
+ email: plashchynski@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - lib/rgb.rb
55
+ - lib/rgb/color.rb
56
+ homepage: https://github.com/plashchynski/rgb
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.25
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A library built to handle the easy conversion, comparison and manipulation
80
+ of colors with CSS-style hex color notation.
81
+ test_files: []