css_color 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +29 -0
- data/lib/css_color.rb +41 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5b23b30a036b3fa34e79313e4db4c7bc2bf0d292
|
4
|
+
data.tar.gz: 1e744c27e37359973be86183a07c11f4b2f4ca97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ebf3b1ec1ab71b0253883c3451af10ba9aadd881073aa1849bb945b4e53f3ca1f565c822f954d49c6b06b02e62f14043f5c1319d288aa847b21f4e4bb33eff9b
|
7
|
+
data.tar.gz: b29ae68f6fab788e61e7671122a94c12c6614cf56bd87c44c78b3a5a40d2f3dd6fb65210d18dea209d853aad95f5a98649e15ac8f0ef6b108b7a47161c2deb90
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
## CSS Color
|
2
|
+
|
3
|
+
Make sense of any css color string.
|
4
|
+
|
5
|
+
The [color gem](https://rubygems.org/gems/color) is fantastic, but it is a requirement that you know what type of colour you're parsing: `#fff`, `blue`, `rgb(0, 0, 0)`, `rgb(50%, 50%, 50%)`
|
6
|
+
|
7
|
+
This gem handles that requirement for you:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require "css_color"
|
11
|
+
|
12
|
+
CSSColor.parse("#fff")
|
13
|
+
#=> RGB [#ffffff]
|
14
|
+
|
15
|
+
CSSColor.parse("blue")
|
16
|
+
#=> RGB [#0000ff]
|
17
|
+
|
18
|
+
CSSColor.parse("rgb(0, 0, 0)")
|
19
|
+
#=> RGB [#000000]
|
20
|
+
|
21
|
+
CSSColor.parse("rgb(50%, 50%, 50%)")
|
22
|
+
#=> RGB [#808080]
|
23
|
+
```
|
24
|
+
|
25
|
+
The return type is an instance of the `Color` class, which has a rich feature set.
|
26
|
+
|
27
|
+
This gem aims to comply with the color grammar defined [here](http://www.w3.org/TR/SVG/types.html#DataTypeColor).
|
28
|
+
|
29
|
+
RGBA, HSL and HSLA are not part of this specification. You're welcome to implement them and send a pull request.
|
data/lib/css_color.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "color"
|
2
|
+
require "color/css"
|
3
|
+
|
4
|
+
class CSSColor
|
5
|
+
|
6
|
+
def self.parse(string)
|
7
|
+
|
8
|
+
case string
|
9
|
+
when /#/
|
10
|
+
Color::RGB.from_html(string)
|
11
|
+
when /%/
|
12
|
+
integers = extract(string, max = 100)
|
13
|
+
Color::RGB.from_percentage(*integers)
|
14
|
+
when /rgb/i
|
15
|
+
integers = extract(string, max = 255)
|
16
|
+
Color::RGB.new(*integers)
|
17
|
+
else
|
18
|
+
Color::CSS[string]
|
19
|
+
end or raise ArgumentError
|
20
|
+
|
21
|
+
rescue ArgumentError
|
22
|
+
|
23
|
+
raise UnknownColorError.new(
|
24
|
+
"Couldn't make sense of #{string.inspect}"
|
25
|
+
)
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def self.extract(string, max)
|
31
|
+
integers = string.scan(/-?\d+/).map(&:to_i)
|
32
|
+
|
33
|
+
raise ArgumentError unless integers.size == 3
|
34
|
+
raise ArgumentError unless integers.all? { |i| (0..max).include?(i) }
|
35
|
+
|
36
|
+
integers
|
37
|
+
end
|
38
|
+
|
39
|
+
class UnknownColorError < ArgumentError; end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: css_color
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Patuzzo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: color
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Make sense of any css color string.
|
42
|
+
email: chris@patuzzo.co.uk
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/css_color.rb
|
49
|
+
homepage: https://github.com/cpatuzzo/css_color
|
50
|
+
licenses: []
|
51
|
+
metadata: {}
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 2.0.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: CSS Color
|
72
|
+
test_files: []
|