Complementary_Color 0.1.1 → 0.1.5

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
  SHA256:
3
- metadata.gz: ec54df84e82c8d5385915c2bf31ad6bb7355221535c7c9124d2b16425dfa1b54
4
- data.tar.gz: d356ac4c22b9d35541323924b5d041e596c368e897ed8ef5f8d5bd2ff22e3707
3
+ metadata.gz: 31fc734fd0d69856e69bf70761225878595b441d53ca0553d9fff8143ca1b018
4
+ data.tar.gz: e9289443a4de434a4c00a46745b3564f237d89f13f39c715fea591d2782efc65
5
5
  SHA512:
6
- metadata.gz: 6117506e43ce939592afb5dc7e5e5e259502fd9b974d2532e53c21ba0b0fc649c00dfb77fafc0e6993f14f8213624da64d1aad9e44479f9257ed66ce74017071
7
- data.tar.gz: c3d5e12cd38c69193a560c76ad7a06b18ca24062fc311ac2fa77a97a99c44d703a4e1aec54e58835a88f3ce0aeeed681a41bcd4d98395059163194bdd6dba60c
6
+ metadata.gz: ad43f1a51ccda5e578a3f330ddbcb309ca801211181c7dc3b67171b39ec06dbd31d37dc0a82d65a59830f3acde0fc21ff38dcc8e36245051bee94cef7e73c425
7
+ data.tar.gz: f8d7b4096909ef0c53d632bfdd89893a64f5bdd6123c08deffa3e0499d8d1473ee9ea9b5c0252554bfbdda2e66f85991a903e0fedd9ec62ecfb9ef1297b1d14d
data/CHANGELOG.md CHANGED
@@ -1,7 +0,0 @@
1
- ## [0.1.1] - 2022-06-24
2
-
3
- - First functionality added
4
-
5
- ## [0.1.0] - 2022-06-21
6
-
7
- - Initial release
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ColoR
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/Complementary_Color.svg)](https://badge.fury.io/rb/Complementary_Color)
4
+ [![Test + Lint](https://github.com/TimHi/Compelementary-Color/actions/workflows/main.yml/badge.svg)](https://github.com/TimHi/Compelementary-Color/actions/workflows/main.yml)
5
+
3
6
  ColoR's main aspect is the creation of complementary color palettes. It also offers RGB to hex conversions.
4
7
 
5
8
  ## Installation
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Error class to capture illegal hex codes being passed to the class.
4
+ class HexCodeError < StandardError
5
+ attr_reader :wrong_color
6
+
7
+ def initialize(wrong_color)
8
+ super
9
+ @wrong_color = wrong_color
10
+ end
11
+ end
data/lib/color/hex.rb CHANGED
@@ -9,7 +9,12 @@ module Hex
9
9
  long_hex_color.downcase
10
10
  end
11
11
 
12
- def self.hex?(color)
13
- color.match("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
12
+ # Test wether a given color is a valid hex color.
13
+ def self.hex?(color_to_test)
14
+ if color_to_test.is_a? String
15
+ color_to_test.match("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
16
+ else
17
+ false
18
+ end
14
19
  end
15
20
  end
data/lib/color/hsv.rb ADDED
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Offering functionality related to the HSV color model.
4
+ module HSV
5
+ HUE_MULTIPLIER = 60
6
+ RGB_MAX_VALUE = 255
7
+ ROUND_VALUE = 3
8
+ FULL_CIRCLE_DEG = 360
9
+ MONO_SATURATION = 0.2
10
+ MONO_VALUE = 0.1
11
+
12
+ # Hex Colors can be represented with three characters, to work
13
+ def self.rgb_to_hsv(rgb_color)
14
+ max = rgb_color.max()
15
+ min = rgb_color.min()
16
+ r = rgb_color[0]
17
+ g = rgb_color[1]
18
+ b = rgb_color[2]
19
+
20
+ value = (max.to_f / RGB_MAX_VALUE).round(ROUND_VALUE)
21
+ max_min_diff = max - min
22
+ hue = calculate_hue(r, g, b, max_min_diff)
23
+ hue += FULL_CIRCLE_DEG if hue.negative?
24
+ [hue.round, calculate_saturation(max_min_diff, max, value), value]
25
+ end
26
+
27
+ def self.calculate_saturation(max_min_diff, max, value)
28
+ saturation = 0
29
+ saturation = (max_min_diff.to_f / max).round(ROUND_VALUE) if value.positive?
30
+ saturation
31
+ end
32
+
33
+ # Method to calculate the hue value of three given rgb colors and the difference
34
+ # of the max - min color.
35
+ def self.calculate_hue(red, green, blue, max_min_diff)
36
+ if red > green && red > blue
37
+ HUE_MULTIPLIER * calculate_raw_hue_value(0, green, blue, max_min_diff)
38
+ elsif green > red && green > blue
39
+ HUE_MULTIPLIER * calculate_raw_hue_value(2, blue, red, max_min_diff)
40
+ elsif blue > red && blue > green
41
+ HUE_MULTIPLIER * calculate_raw_hue_value(4, red, green, max_min_diff)
42
+ else
43
+ 0
44
+ end
45
+ end
46
+
47
+ def self.calculate_raw_hue_value(offset, first_color, second_color, max_min_diff)
48
+ offset + ((first_color - second_color).to_f / max_min_diff).round(ROUND_VALUE)
49
+ end
50
+
51
+ # Calculate the a monochromatic value to a given color
52
+ def self.calculate_monochromatic_color(hue_color)
53
+ saturation = hue_color[1]
54
+ value = hue_color[2]
55
+ if saturation > 0.7
56
+ saturation -= MONO_SATURATION
57
+ else
58
+ saturation += MONO_SATURATION
59
+ end
60
+ value -= MONO_VALUE
61
+ [hue_color[0], saturation.round(ROUND_VALUE), value]
62
+ end
63
+
64
+ # rubocop:disable Metrics/AbcSize
65
+ # rubocop:disable Metrics/MethodLength
66
+ def self.hsv_to_rgb(hue, saturation, value)
67
+ hue = hue.to_f / FULL_CIRCLE_DEG
68
+
69
+ hue_i = (hue * 6).to_i
70
+ f = (hue * 6) - hue_i
71
+ p = value * (1 - saturation)
72
+ q = value * (1 - (f * saturation))
73
+ t = value * (1 - ((1 - f) * saturation))
74
+ if hue_i.zero?
75
+ red = value
76
+ green = t
77
+ blue = p
78
+ end
79
+ if hue_i == 1
80
+ red = q
81
+ green = value
82
+ blue = p
83
+ end
84
+ if hue_i == 2
85
+ red = p
86
+ green = value
87
+ blue = t
88
+ end
89
+ if hue_i == 3
90
+ red = p
91
+ green = q
92
+ blue = value
93
+ end
94
+ if hue_i == 4
95
+ red = t
96
+ green = p
97
+ blue = value
98
+ end
99
+ if hue_i == 5
100
+ red = value
101
+ green = p
102
+ blue = q
103
+ end
104
+ [(red * RGB_MAX_VALUE).round.abs, (green * RGB_MAX_VALUE).round.abs, (blue * RGB_MAX_VALUE).round.abs]
105
+ end
106
+ # rubocop:enable Metrics/AbcSize
107
+ # rubocop:enable Metrics/MethodLength
108
+ end
data/lib/color/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ColoR
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/color.rb CHANGED
@@ -1,21 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "color/version"
4
+ require "color/error/hex_code_error"
4
5
 
5
6
  # Offering functionality to get the complementary colors of a given input.
6
7
  module ColoR
7
8
  class Error < StandardError; end
8
9
 
9
- # Error class to capture illegal hex codes being passed to the class.
10
- class HexCodeError < StandardError
11
- attr_reader :wrong_color
12
-
13
- def initialize(wrong_color)
14
- super
15
- @wrong_color = wrong_color
16
- end
17
- end
18
-
19
10
  def self.get_complementary_color(color)
20
11
  raise HexCodeError.new("Not a valid Hex Color"), color unless ::Hex.hex?(color)
21
12
 
@@ -23,4 +14,14 @@ module ColoR
23
14
  comp_r = [255 - rgb_color[0], 255 - rgb_color[1], 255 - rgb_color[2]]
24
15
  ::RGB.rgb_to_hex(comp_r)
25
16
  end
17
+
18
+ def self.get_monochromatic_color(color)
19
+ raise HexCodeError.new("Not a valid Hex Color"), color unless ::Hex.hex?(color)
20
+
21
+ rgb_color = ::RGB.hex_to_rgb(color)
22
+ hue_color = ::HSV.rgb_to_hsv(rgb_color)
23
+ mono_color = ::HSV.calculate_monochromatic_color(hue_color)
24
+ mono_rgb = ::HSV.hsv_to_rgb(mono_color[0], mono_color[1], mono_color[2])
25
+ ::RGB.rgb_to_hex(mono_rgb)
26
+ end
26
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Complementary_Color
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Hiller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-24 00:00:00.000000000 Z
11
+ date: 2022-06-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Create complementary colors based on a given color.
14
14
  email:
@@ -24,12 +24,15 @@ files:
24
24
  - README.md
25
25
  - Rakefile
26
26
  - lib/color.rb
27
+ - lib/color/error/hex_code_error.rb
27
28
  - lib/color/hex.rb
29
+ - lib/color/hsv.rb
28
30
  - lib/color/rgb.rb
29
31
  - lib/color/version.rb
30
32
  - sig/color.rbs
31
33
  homepage: https://github.com/TimHi/coloR
32
- licenses: []
34
+ licenses:
35
+ - AGPL-3.0-or-later
33
36
  metadata:
34
37
  homepage_uri: https://github.com/TimHi/coloR
35
38
  source_code_uri: https://github.com/TimHi/coloR
@@ -53,5 +56,5 @@ requirements: []
53
56
  rubygems_version: 3.3.16
54
57
  signing_key:
55
58
  specification_version: 4
56
- summary: Create complementary colors based on a given color.
59
+ summary: Generate complementary colors based on a given color.
57
60
  test_files: []