piccolor 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/lib/.DS_Store +0 -0
- data/lib/piccolor/version.rb +3 -0
- data/lib/piccolor/xkcd.txt +196608 -0
- data/lib/piccolor.rb +110 -0
- data/piccolor.gemspec +27 -0
- data/test/hex_test.rb +74 -0
- data/test/test_helper.rb +3 -0
- metadata +114 -0
data/lib/piccolor.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require "color"
|
2
|
+
require "piccolor/version"
|
3
|
+
|
4
|
+
|
5
|
+
module Piccolor
|
6
|
+
@@colors = nil
|
7
|
+
@@corrections = {
|
8
|
+
"dark orange" => "brown",
|
9
|
+
"light maroon" => "red",
|
10
|
+
"light black" => "grey",
|
11
|
+
"dark white" => "grey",
|
12
|
+
"light sky blue" => "light blue",
|
13
|
+
"light cyan" => "light blue",
|
14
|
+
"dark cyan" => "teal",
|
15
|
+
"light red" => "pink",
|
16
|
+
"dark pink" => "purple",
|
17
|
+
"dark yellow" => "brown",
|
18
|
+
"dark lime green" => "green"
|
19
|
+
}
|
20
|
+
|
21
|
+
def self.to_human(hex)
|
22
|
+
correct_rgb = Color::RGB.from_html(hex)
|
23
|
+
correct_hex = correct_rgb.html
|
24
|
+
if @@colors.nil?
|
25
|
+
@@colors = self.full_colors
|
26
|
+
end
|
27
|
+
|
28
|
+
if not @@colors[correct_hex].nil?
|
29
|
+
# yep, this is your colour
|
30
|
+
@@colors[correct_hex]
|
31
|
+
else
|
32
|
+
# okay, some cleverness now
|
33
|
+
correct_hsl = correct_rgb.to_hsl
|
34
|
+
corrected_hsl = correct_hsl.clone
|
35
|
+
corrected_hsl.saturation = 100
|
36
|
+
corrected_hsl.lightness = 50
|
37
|
+
self.desc_correct(@@colors[corrected_hsl.html], correct_hsl.hue, correct_hsl.saturation - 50, correct_hsl.lightness - 50, correct_hex)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.check_greys(word, h, s, l, hex=nil)
|
42
|
+
|
43
|
+
if (s + 50) ** 2 + (l - 50) ** 2 <= 45
|
44
|
+
return "white"
|
45
|
+
elsif l < -38
|
46
|
+
return "black"
|
47
|
+
elsif s < -35 and l <= -16
|
48
|
+
return "dark grey"
|
49
|
+
elsif s < -48 and l < 30 and l > -16
|
50
|
+
return "grey"
|
51
|
+
elsif s < -48 and l >= 30
|
52
|
+
return "light grey"
|
53
|
+
elsif (h < 76 or h > 345) and s < -25 and l > 0
|
54
|
+
return "warm grey"
|
55
|
+
elsif s < -25 and l > 0
|
56
|
+
return "cold grey"
|
57
|
+
else
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.desc_correct(word, h, s, l, hex=nil)
|
63
|
+
# p "#{word}, #{h}, #{s}, #{l}, #{hex}"
|
64
|
+
|
65
|
+
grey = self.check_greys(word, h, s, l, hex)
|
66
|
+
return grey if not grey.nil?
|
67
|
+
|
68
|
+
if l >= -15 and l <= 15
|
69
|
+
self.doubles word
|
70
|
+
elsif l < -15
|
71
|
+
self.word_correct "dark #{word}"
|
72
|
+
elsif l > 15
|
73
|
+
self.word_correct "light #{word}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.doubles(phrase)
|
78
|
+
phrase.gsub("dark light ", "").gsub("light dark ", "")
|
79
|
+
.gsub("light light ", "light ").gsub("dark dark ", "dark ")
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.word_correct(phrase)
|
83
|
+
phrase = self.doubles(phrase)
|
84
|
+
if @@corrections.has_key?(phrase)
|
85
|
+
@@corrections[phrase]
|
86
|
+
else
|
87
|
+
phrase
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.full_colors
|
92
|
+
regexp = /\A\[(\d{1,3}), (\d{1,3}), (\d{1,3})\] ([a-z\s]+)\z/
|
93
|
+
colors = {}
|
94
|
+
|
95
|
+
self.get_text_file.each do |line|
|
96
|
+
|
97
|
+
matched = line.match(regexp)
|
98
|
+
rgb = Color::RGB.new(matched[1].to_i, matched[2].to_i, matched[3].to_i).html()
|
99
|
+
human = matched[4].gsub("\n", "")
|
100
|
+
|
101
|
+
colors[rgb] = human
|
102
|
+
end
|
103
|
+
|
104
|
+
colors
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.get_text_file
|
108
|
+
open(File.dirname(__FILE__) + "/piccolor/xkcd.txt").readlines
|
109
|
+
end
|
110
|
+
end
|
data/piccolor.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'piccolor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "piccolor"
|
8
|
+
spec.version = Piccolor::VERSION
|
9
|
+
spec.authors = ["Rik Lomas"]
|
10
|
+
spec.email = ["lomas.rik@gmail.com"]
|
11
|
+
spec.description = %q{Turning hex colors into real world color names}
|
12
|
+
spec.summary = %q{Turning hex colors into real world color names}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_dependency "color"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
end
|
data/test/hex_test.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe "hex" do
|
4
|
+
|
5
|
+
it "should accept different hex codes" do
|
6
|
+
Piccolor.to_human("#ff0000").must_equal "red"
|
7
|
+
Piccolor.to_human("#f00").must_equal "red"
|
8
|
+
Piccolor.to_human("ff0000").must_equal "red"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should convert whites, greys and blacks" do
|
12
|
+
colors = {
|
13
|
+
"#ffffff" => "white",
|
14
|
+
"#eeeeee" => "white",
|
15
|
+
"#ededed" => "light grey",
|
16
|
+
"#cccccc" => "light grey",
|
17
|
+
"#c1c1c1" => "grey",
|
18
|
+
"#999999" => "grey",
|
19
|
+
"#888888" => "grey",
|
20
|
+
"#777777" => "grey",
|
21
|
+
"#666666" => "grey",
|
22
|
+
"#555555" => "dark grey",
|
23
|
+
"#444444" => "dark grey",
|
24
|
+
"#333333" => "dark grey",
|
25
|
+
"#222222" => "dark grey",
|
26
|
+
"#111111" => "black",
|
27
|
+
"#000000" => "black"
|
28
|
+
}
|
29
|
+
colors.each do |hex, value|
|
30
|
+
Piccolor.to_human(hex).must_equal value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should convert warm and cold greys" do
|
35
|
+
colors = {
|
36
|
+
"#e2d3d3" => "warm grey",
|
37
|
+
"#e2dcd3" => "warm grey",
|
38
|
+
"#bcbcaf" => "warm grey",
|
39
|
+
"#9e9e91" => "warm grey",
|
40
|
+
"#b9c0cb" => "cold grey",
|
41
|
+
"#b9cbbe" => "cold grey"
|
42
|
+
}
|
43
|
+
colors.each do |hex, value|
|
44
|
+
Piccolor.to_human(hex).must_equal value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
it "should convert colors" do
|
50
|
+
colors = {
|
51
|
+
"#00ff00" => "green",
|
52
|
+
"#0000ff" => "blue",
|
53
|
+
"#e80000" => "red",
|
54
|
+
"#0a4a00" => "green",
|
55
|
+
"#730000" => "red",
|
56
|
+
"#ffae00" => "orange",
|
57
|
+
"#030010" => "black",
|
58
|
+
"#402304" => "brown",
|
59
|
+
"#9febf0" => "light blue",
|
60
|
+
"#eb9494" => "pink",
|
61
|
+
"#666a36" => "brown",
|
62
|
+
"#5c2b5d" => "purple",
|
63
|
+
"#070123" => "black",
|
64
|
+
"#043d3c" => "teal",
|
65
|
+
"#c4bab5" => "warm grey",
|
66
|
+
"#ece8e0" => "warm grey"
|
67
|
+
}
|
68
|
+
|
69
|
+
colors.each do |hex, value|
|
70
|
+
Piccolor.to_human(hex).must_equal value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piccolor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rik Lomas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-16 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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Turning hex colors into real world color names
|
70
|
+
email:
|
71
|
+
- lomas.rik@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/.DS_Store
|
82
|
+
- lib/piccolor.rb
|
83
|
+
- lib/piccolor/version.rb
|
84
|
+
- lib/piccolor/xkcd.txt
|
85
|
+
- piccolor.gemspec
|
86
|
+
- test/hex_test.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.1.11
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Turning hex colors into real world color names
|
112
|
+
test_files:
|
113
|
+
- test/hex_test.rb
|
114
|
+
- test/test_helper.rb
|