css-colour 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f6f5160b9147fdb467f57053b4409e5bcad6be0
4
+ data.tar.gz: 3242f8a8af232b6ee693589c84ea542fdbd6cd2a
5
+ SHA512:
6
+ metadata.gz: 6e5087cf210526af17d4d4cd5428bbefcc54e59a742d86d521ebcbee26e76b3c14d672664fc958da2f565c618e09d6fd81457db023255bdccb3b2820c8d124ea
7
+ data.tar.gz: ffc1268bd37bfe4e38da07f8154b0556ee6835a931c7490fe4a4534550119fef026c5b23d0c5d8632eae4c59611446ad4e93b8daf66b550ea9b2ace0a6f5dea4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in css-colour.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Darío Javier Cravero
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # CssColour
2
+
3
+ Ruby CSS Colour validator.
4
+
5
+ It will tell if a colour is either:
6
+
7
+ * inherit,
8
+ * HEX `#123` or `#123123`,
9
+ * HSL,
10
+ * HSLA,
11
+ * RGB or
12
+ * RGBA
13
+
14
+ A couple of guidelines were used here:
15
+
16
+ * [MDN's docs](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Color),
17
+ * [this Ruby HEX validator](https://gist.github.com/liquid/1324384) and
18
+ * [these Java regexes for HSL/RGB/RGBA were adapted to Ruby and made more readable](http://stackoverflow.com/a/12457269/1562732).
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```
25
+ gem 'css-colour'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ ```
31
+ $ bundle
32
+ ```
33
+
34
+ Or install it yourself as:
35
+
36
+ ```
37
+ $ gem install css-colour
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```
43
+ validator = CssColour::Validator.new 'inherit'
44
+ validator.valid? # true
45
+ ```
46
+
47
+ You can also test against a specific colour type as follows:
48
+
49
+ ```
50
+ validator = CssColour::Validator.new '#123456'
51
+ validator.rgb? # false
52
+ ```
53
+
54
+ ## TODO
55
+
56
+ * Try to fix a colour.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( http://github.com/dariocravero/css-colour/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'css-colour/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "css-colour"
8
+ spec.version = CssColour::VERSION
9
+ spec.authors = ["Darío Javier Cravero"]
10
+ spec.email = ["dario@uxtemple.com"]
11
+ spec.summary = %q{CSS Colour validator.}
12
+ spec.description = %q{
13
+ Ruby CSS Colour validator.
14
+
15
+ It will tell if a colour is either:
16
+
17
+ * inherit,
18
+ * HEX `#123` or `#123123`,
19
+ * HSL,
20
+ * HSLA,
21
+ * RGB or
22
+ * RGBA
23
+ }
24
+ spec.homepage = "https://github.com/dariocravero/css-colour"
25
+ spec.license = "MIT"
26
+
27
+ spec.files = `git ls-files -z`.split("\x0")
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.5"
33
+ spec.add_development_dependency "rake", '~> 0'
34
+ spec.add_development_dependency "minitest", '~> 0'
35
+ end
@@ -0,0 +1,64 @@
1
+ module CssColour
2
+ module Constants
3
+ SPACE = /\s/
4
+
5
+ NO_SPACE = ''.freeze
6
+
7
+ INHERIT = 'inherit'.freeze
8
+
9
+ WEBSAFE = %w(aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue
10
+ blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan
11
+ darkblue darkcyan darkgoldenrod darkgray darkgrey darkgreen darkkhaki darkmagenta darkolivegreen
12
+ darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey
13
+ darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite
14
+ forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray grey green greenyellow honeydew
15
+ hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue
16
+ lightcoral lightcyan lightgoldenrodyellow lightgray lightgrey lightgreen lightpink lightsalmon
17
+ lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime
18
+ limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple
19
+ mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue
20
+ mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid
21
+ palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum
22
+ powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell
23
+ sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal
24
+ thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen).freeze
25
+
26
+ HEX = /^#(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/
27
+
28
+ RANGE_0_360 = %r{
29
+ 0| # 0
30
+ [1-9]\d?| # 1-9 and 10-99
31
+ [12]\d\d?| # 100-299
32
+ 3[0-5]\d| # 300-359
33
+ 360 # 360
34
+ }x
35
+
36
+ RANGE_0_100 = %r{
37
+ 0| # 0
38
+ [1-9]\d?| # 1-9 and 10-99
39
+ 100 # 100
40
+ }x
41
+
42
+ RANGE_0_255 = %r{
43
+ 0| # 0
44
+ [1-9]\d?| # 1-9 and 10-99
45
+ 1\d\d?| # 100-199
46
+ 2[0-4]\d| # 200-249
47
+ 25[0-5] # 250-255
48
+ }x
49
+
50
+ RANGE_ALPHA = %r{
51
+ (0\.\d)| # 0.0-0.9
52
+ (1\.0)| # 1.0
53
+ [01] # 0 or 1
54
+ }x
55
+
56
+ HSL = /^hsl\((#{RANGE_0_360}),(#{RANGE_0_100}%),(#{RANGE_0_100}%)\)$/
57
+
58
+ HSLA = /^hsla\((#{RANGE_0_360}),(#{RANGE_0_100}%),(#{RANGE_0_100}%),(#{RANGE_ALPHA})\)$/
59
+
60
+ RGB = /^rgb\((#{RANGE_0_255}),(#{RANGE_0_255}),(#{RANGE_0_255})\)$/
61
+
62
+ RGBA = /^rgba\((#{RANGE_0_255}),(#{RANGE_0_255}),(#{RANGE_0_255}),(#{RANGE_ALPHA})\)$/
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module CssColour
2
+ VERSION = "0.0.1"
3
+ end
data/lib/css-colour.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'css-colour/constants'
2
+ require 'css-colour/version'
3
+
4
+ module CssColour
5
+ class Validator
6
+ include CssColour::Constants
7
+
8
+ attr_accessor :colour
9
+
10
+ def initialize(value); self.colour = value; end
11
+
12
+ def colour=(value); @colour = clean(value); end
13
+ def clean(value); value.downcase.gsub(SPACE, NO_SPACE); end
14
+
15
+ def valid?; inherit? or websafe? or hex? or rgb? or rgba? or hsl? or hsla?; end
16
+
17
+ def inherit?; @colour == INHERIT; end
18
+ def websafe?; WEBSAFE.include? @colour; end
19
+ def hex?; is? HEX; end
20
+ def hsl?; is? HSL; end
21
+ def hsla?; is? HSLA; end
22
+ def rgb?; is? RGB; end
23
+ def rgba?; is? RGBA; end
24
+ private def is?(type); !type.match(@colour).nil?; end
25
+ end
26
+ end
@@ -0,0 +1,77 @@
1
+ require 'css-colour'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+
5
+ describe 'validator test' do
6
+ it 'should validate inherit' do
7
+ assert CssColour::Validator.new('inherit').valid?
8
+ end
9
+
10
+ it 'should validate websafe colours' do
11
+ assert CssColour::Validator.new('white').valid?
12
+ assert CssColour::Validator.new('White').valid?
13
+ refute CssColour::Validator.new('blanco').valid?
14
+ end
15
+
16
+ it 'should validate HEX colours' do
17
+ assert CssColour::Validator.new('#123456').valid?
18
+ assert CssColour::Validator.new('#123').valid?
19
+ assert CssColour::Validator.new('#FF1234').valid?
20
+ refute CssColour::Validator.new('123456').valid?
21
+ refute CssColour::Validator.new('123').valid?
22
+ refute CssColour::Validator.new('#GG0000').valid?
23
+ refute CssColour::Validator.new('#FF00001').valid?
24
+ end
25
+
26
+ it 'should validate HSL colours' do
27
+ assert CssColour::Validator.new('hsl(0,0%,0%)').valid?
28
+ assert CssColour::Validator.new('hsl(360,100%,100%)').valid?
29
+ refute CssColour::Validator.new('hsl 0,0%,0%)').valid?
30
+ refute CssColour::Validator.new('hsl(0,0%,0%').valid?
31
+ refute CssColour::Validator.new('hsl(0,0,0)').valid?
32
+ refute CssColour::Validator.new('hsl(360,100,100)').valid?
33
+ refute CssColour::Validator.new('hsl(0,150%,150%)').valid?
34
+ refute CssColour::Validator.new('hsl(-0,-0%,-0%)').valid?
35
+ end
36
+
37
+ it 'should validate HSLA colours' do
38
+ assert CssColour::Validator.new('hsla(0,0%,0%,0)').valid?
39
+ assert CssColour::Validator.new('hsla(0,0%,0%,0.0)').valid?
40
+ assert CssColour::Validator.new('hsla(360,100%,100%,1)').valid?
41
+ assert CssColour::Validator.new('hsla(360,100%,100%,1.0)').valid?
42
+ refute CssColour::Validator.new('hsla 0,0%,0%,0.0)').valid?
43
+ refute CssColour::Validator.new('hsla(0,0%,0%,0,0)').valid?
44
+ refute CssColour::Validator.new('hsla(0,0%,0%,0.0 ').valid?
45
+ refute CssColour::Validator.new('hsla(0,0%,0%)').valid?
46
+ refute CssColour::Validator.new('hsla(360,100%,100%)').valid?
47
+ refute CssColour::Validator.new('hsla(360,100%,100%,-0)').valid?
48
+ refute CssColour::Validator.new('hsla(360,100%,100%,1.1)').valid?
49
+ end
50
+
51
+ it 'should validate RGB colours' do
52
+ assert CssColour::Validator.new('rgb(0,0,0)').valid?
53
+ assert CssColour::Validator.new('rgb(255,255,255)').valid?
54
+ refute CssColour::Validator.new('rgb 0,0,0)').valid?
55
+ refute CssColour::Validator.new('rgb(0,0,0 ').valid?
56
+ refute CssColour::Validator.new('rgb(-0,0,0)').valid?
57
+ refute CssColour::Validator.new('rgb(256,0,0)').valid?
58
+ refute CssColour::Validator.new('rgb(0,-0,0)').valid?
59
+ refute CssColour::Validator.new('rgb(0,256,0)').valid?
60
+ refute CssColour::Validator.new('rgb(0,0,-0)').valid?
61
+ refute CssColour::Validator.new('rgb(0,0,256)').valid?
62
+ end
63
+
64
+ it 'should validate RGBA colours' do
65
+ assert CssColour::Validator.new('rgba(0,0,0,0)').valid?
66
+ assert CssColour::Validator.new('rgba(0,0,0,0.0)').valid?
67
+ assert CssColour::Validator.new('rgba(255,255,255,1)').valid?
68
+ assert CssColour::Validator.new('rgba(255,255,255,1.0)').valid?
69
+ refute CssColour::Validator.new('rgba 0,0,0,0.0)').valid?
70
+ refute CssColour::Validator.new('rgba(0,0,0,0,0)').valid?
71
+ refute CssColour::Validator.new('rgba(0,0,0,0.0 ').valid?
72
+ refute CssColour::Validator.new('rgba(0,0,0)').valid?
73
+ refute CssColour::Validator.new('rgba(255,255,255)').valid?
74
+ refute CssColour::Validator.new('rgba(0,0,0,-0)').valid?
75
+ refute CssColour::Validator.new('rgba(0,0,0,1.1)').valid?
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css-colour
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Darío Javier Cravero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "\n Ruby CSS Colour validator.\n\n It will tell if a colour is
56
+ either:\n\n * inherit,\n * HEX `#123` or `#123123`,\n * HSL,\n * HSLA,\n
57
+ \ * RGB or\n * RGBA\n "
58
+ email:
59
+ - dario@uxtemple.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - css-colour.gemspec
70
+ - lib/css-colour.rb
71
+ - lib/css-colour/constants.rb
72
+ - lib/css-colour/version.rb
73
+ - test/validator_test.rb
74
+ homepage: https://github.com/dariocravero/css-colour
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: CSS Colour validator.
98
+ test_files:
99
+ - test/validator_test.rb
100
+ has_rdoc: