color_contrast_calc 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 513b56ff5dca5ea3a6a7343a4cc5111a19c976e2
4
- data.tar.gz: 9958f22f8d1474b93a3573ce728ffe6e41abe6d7
3
+ metadata.gz: 5a62e36d449991a9381385e68fcf0c2e8201bdbc
4
+ data.tar.gz: 1053f1e6a889f8062ab53de73eaf1f92c434785f
5
5
  SHA512:
6
- metadata.gz: ebf3189b2a716538c29fe3c3730e47f79d9a51fba4d53989269fe20f9a56e0225ee38e105936aa2c05844b3fb4925243feeefa9c670733975f82b46eabb15c2d
7
- data.tar.gz: 5c6b613c861aaa290e31d1c1c710c6d7775beb742f43c896f25e7c10bf1b890cc7833ebe7f37b8b4c5727bc1ce68eb4ad9c8916f8bc853fc8f47d405ac2d34ec
6
+ metadata.gz: 1b51b39ef9e836dcf205883dba7a3ee96a3844eab201e799a012e22e85e88a4c69cdbe03d11f7d5f9fa1069c972034496c6c1b8e30d2a90b98b8bce04932d780
7
+ data.tar.gz: 49891f3150bdb8bf43831efe09150889109b9eaf9d0244ec5a4101d07dd77878caf82faa4831ea3779224ae46895daeebcef351ad38c9b958544ee7a24c3554d
@@ -9,33 +9,22 @@ require 'color_contrast_calc/color'
9
9
  require 'color_contrast_calc/sorter'
10
10
 
11
11
  module ColorContrastCalc
12
- ##
13
- # Error raised if creating a Color instance with invalid value.
14
-
15
- class InvalidColorRepresentationError < StandardError; end
16
-
17
12
  ##
18
13
  # Return an instance of Color.
19
14
  #
20
15
  # As +color_value+, you can pass a predefined color name, or an
21
16
  # RGB value represented as an array of integers or a hex code such
22
17
  # as [255, 255, 0] or "#ffff00". +name+ is assigned to the returned
23
- # instance if it does not have a name already assigned.
18
+ # instance.
24
19
  # @param color_value [String, Array<Integer>] Name of a predefined
25
- # color or RGB value
26
- # @param name [String] Unless the instance has predefined name, the
27
- # name passed to the method is set to self.name
20
+ # color, hex color code or RGB value
21
+ # @param name [String] Without specifying a name, a color keyword name
22
+ # (if exists) or the value of normalized hex color code is assigned
23
+ # to Color#name
28
24
  # @return [Color] Instance of Color
29
25
 
30
26
  def self.color_from(color_value, name = nil)
31
- error_message = 'A color should be given as an array or string.'
32
-
33
- if !color_value.is_a?(String) && !color_value.is_a?(Array)
34
- raise InvalidColorRepresentationError, error_message
35
- end
36
-
37
- return color_from_rgb(color_value, name) if color_value.is_a?(Array)
38
- color_from_str(color_value, name)
27
+ Color.color_from(color_value, name)
39
28
  end
40
29
 
41
30
  ##
@@ -101,33 +90,4 @@ module ColorContrastCalc
101
90
  def self.hsl_colors(s: 100, l: 50, h_interval: 1)
102
91
  Color::List.hsl_colors(s: s, l: l, h_interval: h_interval)
103
92
  end
104
-
105
- def self.color_from_rgb(color_value, name = nil)
106
- error_message = 'An RGB value should be given in form of [r, g, b].'
107
-
108
- unless Utils.valid_rgb?(color_value)
109
- raise InvalidColorRepresentationError, error_message
110
- end
111
-
112
- hex_code = Utils.rgb_to_hex(color_value)
113
- Color::List::HEX_TO_COLOR[hex_code] || Color.new(color_value, name)
114
- end
115
-
116
- private_class_method :color_from_rgb
117
-
118
- def self.color_from_str(color_value, name = nil)
119
- error_message = 'A hex code is in form of "#xxxxxx" where 0 <= x <= f.'
120
-
121
- named_color = Color::List::NAME_TO_COLOR[color_value]
122
- return named_color if named_color
123
-
124
- unless Utils.valid_hex?(color_value)
125
- raise InvalidColorRepresentationError, error_message
126
- end
127
-
128
- hex_code = Utils.normalize_hex(color_value)
129
- Color::List::HEX_TO_COLOR[hex_code] || Color.new(hex_code, name)
130
- end
131
-
132
- private_class_method :color_from_str
133
93
  end
@@ -7,6 +7,11 @@ require 'color_contrast_calc/deprecated'
7
7
  require 'json'
8
8
 
9
9
  module ColorContrastCalc
10
+ ##
11
+ # Error raised if creating a Color instance with invalid value.
12
+
13
+ class InvalidColorRepresentationError < StandardError; end
14
+
10
15
  ##
11
16
  # Represent specific colors.
12
17
  #
@@ -15,6 +20,144 @@ module ColorContrastCalc
15
20
 
16
21
  class Color
17
22
  include Deprecated::Color
23
+
24
+ ##
25
+ # Module that implements class methods of Color
26
+
27
+ module Factory
28
+ include Deprecated::Color::Factory
29
+
30
+ ##
31
+ # Return an instance of Color for a predefined color name.
32
+ #
33
+ # Color names are defined at
34
+ # * {https://www.w3.org/TR/SVG/types.html#ColorKeywords}
35
+ # @param name [String] Name of color
36
+ # @return [Color] Instance of Color
37
+
38
+ def from_name(name)
39
+ List::NAME_TO_COLOR[name.downcase]
40
+ end
41
+
42
+ ##
43
+ # Return an instance of Color for an RGB value
44
+ #
45
+ # @param rgb [Array<Integer>] RGB value represented as an
46
+ # array of integers such as [255, 255, 0]
47
+ # @param name [String] You can name the color to be created
48
+ # @return [Color] Instance of Color
49
+
50
+ def from_rgb(rgb, name = nil)
51
+ !name && List::HEX_TO_COLOR[Utils.rgb_to_hex(rgb)] ||
52
+ Color.new(rgb, name)
53
+ end
54
+
55
+ ##
56
+ # Return an instance of Color for a hex color code.
57
+ #
58
+ # @param hex [String] Hex color code such as "#ffff00"
59
+ # @param name [String] You can name the color to be created
60
+ # @return [Color] Instance of Color
61
+
62
+ def from_hex(hex, name = nil)
63
+ normalized_hex = Utils.normalize_hex(hex)
64
+ !name && List::HEX_TO_COLOR[normalized_hex] ||
65
+ Color.new(normalized_hex, name)
66
+ end
67
+
68
+ ##
69
+ # Return an instance of Color from an HSL value.
70
+ #
71
+ # @param hsl [Float] HSL value represented as an array of numbers
72
+ # @param name [String] You can name the color to be created
73
+ # @return [Color] Instance of Color
74
+
75
+ def from_hsl(hsl, name = nil)
76
+ rgb = Utils.hsl_to_rgb(hsl)
77
+ !name && List::HEX_TO_COLOR[Utils.rgb_to_hex(rgb)] ||
78
+ Color.new(rgb, name)
79
+ end
80
+
81
+ ##
82
+ # Return an instance of Color.
83
+ #
84
+ # As +color_value+, you can pass a predefined color name, or an
85
+ # RGB value represented as an array of integers or a hex code such
86
+ # as [255, 255, 0] or "#ffff00". +name+ is assigned to the returned
87
+ # instance.
88
+ # @param color_value [String, Array<Integer>] Name of a predefined
89
+ # color, hex color code or RGB value
90
+ # @param name [String] Without specifying a name, a color keyword name
91
+ # (if exists) or the value of normalized hex color code is assigned
92
+ # to Color#name
93
+ # @return [Color] Instance of Color
94
+
95
+ def color_from(color_value, name = nil)
96
+ error_message = 'A color should be given as an array or string.'
97
+
98
+ if !color_value.is_a?(String) && !color_value.is_a?(Array)
99
+ raise InvalidColorRepresentationError, error_message
100
+ end
101
+
102
+ return color_from_rgb(color_value, name) if color_value.is_a?(Array)
103
+ color_from_str(color_value, name)
104
+ end
105
+
106
+ ##
107
+ # Return an instance of Color.
108
+ #
109
+ # As +color_value+, you can pass a Color instance, a predefined color
110
+ # name, or an RGB value represented as an array of integers or a hex
111
+ # code such as [255, 255, 0] or "#ffff00". +name+ is assigned to the
112
+ # returned instance.
113
+ # @param color_value [Color, String, Array<Integer>] An instance of
114
+ # Color, a name of a predefined, color, hex color code or RGB value
115
+ # @param name [String] Without specifying a name, a color keyword name
116
+ # (if exists) or the value of normalized hex color code is assigned
117
+ # to Color#name
118
+ # @return [Color] Instance of Color
119
+
120
+ def as_color(color_value, name = nil)
121
+ if color_value.is_a? Color
122
+ return color_value if color_value.name == name
123
+ color_value = color_value.rgb
124
+ end
125
+
126
+ color_from(color_value, name)
127
+ end
128
+
129
+ def color_from_rgb(rgb_value, name = nil)
130
+ error_message = 'An RGB value should be given in form of [r, g, b].'
131
+
132
+ unless Utils.valid_rgb?(rgb_value)
133
+ raise InvalidColorRepresentationError, error_message
134
+ end
135
+
136
+ hex_code = Utils.rgb_to_hex(rgb_value)
137
+ !name && List::HEX_TO_COLOR[hex_code] || Color.new(rgb_value, name)
138
+ end
139
+
140
+ private :color_from_rgb
141
+
142
+ def color_from_str(color_value, name = nil)
143
+ error_message = 'A hex code is in form of "#xxxxxx" where 0 <= x <= f.'
144
+
145
+ named_color = !name && List::NAME_TO_COLOR[color_value]
146
+ return named_color if named_color
147
+
148
+ unless Utils.valid_hex?(color_value)
149
+ raise InvalidColorRepresentationError, error_message
150
+ end
151
+
152
+ hex_code = Utils.normalize_hex(color_value)
153
+ !name && List::HEX_TO_COLOR[hex_code] || Color.new(hex_code, name)
154
+ end
155
+
156
+ private :color_from_str
157
+ end
158
+
159
+ extend Factory
160
+
18
161
  # @private
19
162
  RGB_LIMITS = [0, 255].freeze
20
163
 
@@ -30,54 +173,20 @@ module ColorContrastCalc
30
173
 
31
174
  attr_reader :rgb, :hex, :name, :relative_luminance
32
175
 
33
- ##
34
- # Return an instance of Color for a predefined color name.
35
- #
36
- # Color names are defined at
37
- # * {https://www.w3.org/TR/SVG/types.html#ColorKeywords}
38
- # @param name [String] Name of color
39
- # @return [Color] Instance of Color
40
-
41
- def self.from_name(name)
42
- List::NAME_TO_COLOR[name.downcase]
43
- end
44
-
45
- ##
46
- # Return an instance of Color for a hex color code.
47
- #
48
- # @param hex [String] Hex color code such as "#ffff00"
49
- # @return [Color] Instance of Color
50
-
51
- def self.from_hex(hex)
52
- normalized_hex = Utils.normalize_hex(hex)
53
- List::HEX_TO_COLOR[normalized_hex] || Color.new(normalized_hex)
54
- end
55
-
56
- ##
57
- # Create an instance of Color from an HSL value.
58
- #
59
- # @param hsl [Float] HSL value represented as an array of numbers
60
- # @param name [String] You can name the color to be created
61
- # @return [Color] Instance of Color
62
-
63
- def self.new_from_hsl(hsl, name = nil)
64
- new(Utils.hsl_to_rgb(hsl), name)
65
- end
66
-
67
176
  ##
68
177
  # Create a new instance of Color.
69
178
  #
70
179
  # @param rgb [Array<Integer>, String] RGB value represented as an array
71
180
  # of integers or hex color code such as [255, 255, 0] or "#ffff00".
72
181
  # @param name [String] You can name the color to be created.
73
- # Without this option, the value of normalized hex color code is
74
- # assigned instead.
182
+ # Without this option, a color keyword name (if exists) or the value
183
+ # of normalized hex color code is assigned instead.
75
184
  # @return [Color] New instance of Color
76
185
 
77
186
  def initialize(rgb, name = nil)
78
187
  @rgb = rgb.is_a?(String) ? Utils.hex_to_rgb(rgb) : rgb
79
188
  @hex = Utils.rgb_to_hex(@rgb)
80
- @name = name || @hex
189
+ @name = name || common_name
81
190
  @relative_luminance = Checker.relative_luminance(@rgb)
82
191
  end
83
192
 
@@ -85,7 +194,7 @@ module ColorContrastCalc
85
194
  # Return HSL value of the color.
86
195
  #
87
196
  # The value is calculated from the RGB value, so if you create
88
- # the instance by Color.new_from_hsl method, the value used to
197
+ # the instance by Color.from_hsl method, the value used to
89
198
  # create the color does not necessarily correspond to the value
90
199
  # of this property.
91
200
  #
@@ -95,6 +204,18 @@ module ColorContrastCalc
95
204
  @hsl ||= Utils.rgb_to_hsl(@rgb)
96
205
  end
97
206
 
207
+ ##
208
+ # Return a {https://www.w3.org/TR/SVG/types.html#ColorKeywords
209
+ # color keyword name} when the name corresponds to the hex code
210
+ # of the color. Otherwise the hex code will be returned.
211
+ #
212
+ # @return [String] Color keyword name or hex color code
213
+
214
+ def common_name
215
+ named_color = List::HEX_TO_COLOR[@hex]
216
+ named_color && named_color.name || @hex
217
+ end
218
+
98
219
  ##
99
220
  # Return a new instance of Color with adjusted contrast.
100
221
  #
@@ -402,7 +523,7 @@ module ColorContrastCalc
402
523
  # saturation and lightness
403
524
 
404
525
  def self.hsl_colors(s: 100, l: 50, h_interval: 1)
405
- 0.step(360, h_interval).map {|h| Color.new_from_hsl([h, s, l]) }.freeze
526
+ 0.step(360, h_interval).map {|h| Color.from_hsl([h, s, l]) }.freeze
406
527
  end
407
528
  end
408
529
 
@@ -33,7 +33,7 @@ module ColorContrastCalc
33
33
  ##
34
34
  # Return contrast adjusted RGB value of passed color.
35
35
  #
36
- # THe calculation is based on the definition found at
36
+ # The calculation is based on the definition found at
37
37
  # https://www.w3.org/TR/filter-effects/#funcdef-contrast
38
38
  # https://www.w3.org/TR/SVG/filters.html#TransferFunctionElementAttributes
39
39
  # @param rgb [Array<Integer>] The Original RGB value before the adjustment
@@ -50,7 +50,7 @@ module ColorContrastCalc
50
50
  ##
51
51
  # Return brightness adjusted RGB value of passed color.
52
52
  #
53
- # THe calculation is based on the definition found at
53
+ # The calculation is based on the definition found at
54
54
  # https://www.w3.org/TR/filter-effects/#funcdef-brightness
55
55
  # https://www.w3.org/TR/SVG/filters.html#TransferFunctionElementAttributes
56
56
  # @param rgb [Array<Integer>] The Original RGB value before the adjustment
@@ -100,7 +100,7 @@ module ColorContrastCalc
100
100
  ##
101
101
  # Return a hue rotation applied RGB value of passed color.
102
102
  #
103
- # THe calculation is based on the definition found at
103
+ # The calculation is based on the definition found at
104
104
  # https://www.w3.org/TR/filter-effects/#funcdef-hue-rotate
105
105
  # https://www.w3.org/TR/SVG/filters.html#TransferFunctionElementAttributes
106
106
  # @param rgb [Array<Integer>] The Original RGB value before the rotation
@@ -166,7 +166,7 @@ module ColorContrastCalc
166
166
  [-0.2126, -0.7152, 0.9278]]
167
167
 
168
168
  ##
169
- # Convert passed a passed color to grayscale.
169
+ # Convert a passed color to grayscale.
170
170
  #
171
171
  # The calculation is based on the definition found at
172
172
  # https://www.w3.org/TR/filter-effects/#funcdef-grayscale
@@ -10,6 +10,15 @@ module ColorContrastCalc
10
10
  end
11
11
 
12
12
  module Color
13
+ module Factory
14
+ ##
15
+ # @deprecated Use Color.from_hsl() instead.
16
+ def new_from_hsl(hsl, name = nil)
17
+ Deprecated.warn(__method__, :from_hsl)
18
+ new(Utils.hsl_to_rgb(hsl), name)
19
+ end
20
+ end
21
+
13
22
  # @deprecated Use {#with_contrast} instead
14
23
  def new_contrast_color(ratio, name = nil)
15
24
  Deprecated.warn(__method__, :with_contrast)
@@ -11,9 +11,7 @@ module ColorContrastCalc
11
11
  # and the following implementation does not satisfy
12
12
  # the full specification of the original method.
13
13
 
14
- def match?(str)
15
- self === str
16
- end
14
+ alias_method(:match?, :===)
17
15
  end
18
16
 
19
17
  refine Numeric do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ColorContrastCalc
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_contrast_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HASHIMOTO, Naoki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-22 00:00:00.000000000 Z
11
+ date: 2018-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler