colos 0.0.1 → 0.0.2

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. checksums.yaml +7 -0
  2. data/lib/colos/version.rb +1 -1
  3. data/lib/colos.rb +101 -11
  4. metadata +7 -8
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e6424cc9978277f08a82c4c77b7492f2ae77d4d
4
+ data.tar.gz: a3da18b9807f7b19293b6775579f1b05d4db3f19
5
+ SHA512:
6
+ metadata.gz: e310d4c28edc50308834f3e1898c574f185e92b231701398d4934b7270afdfd90eeaec1614952effdf8c7b4a5c2f7571088a15d2d6bc4b12ca707525a9e773bf
7
+ data.tar.gz: 75bafe57bf769401eff1be225e1fc2355abd158f6b4d80310386aff9f5bfa867efe5359c22edc9e9d8226f8681087da7dfe5f4284cd091acf6e23a79a8d264b7
data/lib/colos/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Colos
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/colos.rb CHANGED
@@ -1,26 +1,116 @@
1
1
  require "cityhash"
2
2
 
3
3
  class Colos
4
+
5
+ # Options store
6
+ attr_accessor :options
4
7
 
5
- # Colos.color "example"
6
- # => 123123
7
- # Alias for Colos.new
8
- def self.color str
8
+ # Initializer
9
+ #
10
+ # Example:
11
+ # >> Colos.new frequency: 1.2, randomizr: 12
12
+ # => Colos instance
13
+ #
14
+ # Arguments:
15
+ # options: (Hash)
16
+ def initialize options = {}
17
+ default_options = {
18
+ hash: CityHash,
19
+ frequency: 1.4,
20
+ randomizr: 0
21
+ }
22
+
23
+ # merge
24
+ @options = default_options.merge options
25
+
26
+ # errors
27
+ raise "Frequency option should be from 1.2 to 1.6" unless (1.2..1.6).include? @options[:frequency]
28
+ raise "Randomizr option should be from 0 to 23" unless (0..23).include? @options[:randomizr]
29
+ raise "Hash class should have `new` method" unless @options[:hash].respond_to?('hash32')
30
+ end
31
+
32
+ # Convert IPv4 to color
33
+ #
34
+ # Example:
35
+ # >> Colos.ip '173.194.32.14'
36
+ # => "395e9a"
37
+ #
38
+ # Arguments:
39
+ # str: (String)
40
+ def ip str
41
+ ip = str.split '.'
42
+ hex = ""
43
+
44
+ raise "Invalid IP" unless ip.size == 4
45
+
46
+ rgb = ip.permutation(3).to_a[@options[:randomizr]].map &:to_i
47
+
48
+ rgb.each do |b|
49
+ b = b.to_s 16
50
+ b *= 2 unless b.size == 2
51
+ hex += b
52
+ end
53
+
54
+ hex
55
+ end
56
+
57
+ # Convert IPv4 to colors
58
+ #
59
+ # Example:
60
+ # >> Colos.ips '173.194.32.14'
61
+ # => ["adc220", ..., "ee20c2"]
62
+ #
63
+ # Arguments:
64
+ # str: (String)
65
+ def ips str
66
+ ip = str.split '.'
67
+ hexs = []
68
+
69
+ raise "Invalid IP" unless ip.size == 4
70
+
71
+ ip.permutation(3).to_a.each do |color|
72
+ hex = ""
73
+ color.each do |b|
74
+ b = b.to_i.to_s 16
75
+ b *= 2 unless b.size == 2
76
+ hex += b
77
+ end
78
+ hexs << hex
79
+ end
80
+
81
+ hexs
82
+ end
83
+
84
+ # Convert string to color
85
+ #
86
+ # Example:
87
+ # >> Colos.color("word")
88
+ # => "395e9a"
89
+ #
90
+ # Arguments:
91
+ # str: (String)
92
+ def color str
9
93
  hash = CityHash.hash32 str
10
- frequency = 1.4
11
- (hash ** (1.0/frequency)).to_i.to_s 16
94
+ (hash**(1.0 / @options[:frequency])).to_i.to_s 16
12
95
  end
13
96
 
14
- # Colos.colors "example"
15
- # => [123123, 234234]
16
- def self.colors str
97
+ # Convert string to colors
98
+ #
99
+ # Example:
100
+ # >> Colos.colors("word")
101
+ # => ["b7d989", "395e9a", "14e8c0"]
102
+ #
103
+ # Arguments:
104
+ # str: (String)
105
+ def colors str
17
106
  hash = CityHash.hash32 str
18
107
  colors = []
108
+
19
109
  for i in (12..16) do
20
- frequency = i / 10.0
21
- q = (hash ** (1.0/frequency)).to_i
110
+ q = (hash**(1.0 / (i / 10.0))).to_i
22
111
  colors << q.to_s(16) if q.to_s(16).size == 6
23
112
  end
113
+
24
114
  colors
25
115
  end
26
116
 
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Yuri Artemev
@@ -22,26 +21,26 @@ files:
22
21
  - lib/colos/version.rb
23
22
  homepage: http://github.com/artemeff/colos
24
23
  licenses: []
24
+ metadata: {}
25
25
  post_install_message:
26
26
  rdoc_options: []
27
27
  require_paths:
28
28
  - lib
29
29
  required_ruby_version: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- none: false
35
34
  required_rubygems_version: !ruby/object:Gem::Requirement
36
35
  requirements:
37
- - - ! '>='
36
+ - - '>='
38
37
  - !ruby/object:Gem::Version
39
38
  version: '0'
40
- none: false
41
39
  requirements: []
42
40
  rubyforge_project:
43
- rubygems_version: 1.8.24
41
+ rubygems_version: 2.0.0
44
42
  signing_key:
45
- specification_version: 3
43
+ specification_version: 4
46
44
  summary: string to color
47
45
  test_files: []
46
+ has_rdoc: