Module: HSV
- Defined in:
- lib/color/hsv.rb
Overview
Offering functionality related to the HSV color model.
Constant Summary collapse
- HUE_MULTIPLIER =
60- RGB_MAX_VALUE =
255- ROUND_VALUE =
3- FULL_CIRCLE_DEG =
360- MONO_SATURATION =
0.2- MONO_VALUE =
0.1
Class Method Summary collapse
-
.add_hue_to_hsv(hue_value, hsv_color_to_add) ⇒ Object
Add a value to the hue component of a hsv color Ensures that the hue is still in the range 0..360.
-
.calculate_hue(red, green, blue, max_min_diff) ⇒ Object
Method to calculate the hue value of three given rgb colors and the difference of the max - min color.
-
.calculate_monochromatic_color(hue_color) ⇒ Object
Calculate the a monochromatic value to a given color.
- .calculate_raw_hue_value(offset, first_color, second_color, max_min_diff) ⇒ Object
- .calculate_saturation(max_min_diff, max, value) ⇒ Object
-
.hsv_to_rgb(hue, saturation, value) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
-
.rgb_to_hsv(rgb_color) ⇒ Object
Hex Colors can be represented with three characters, to work.
Class Method Details
.add_hue_to_hsv(hue_value, hsv_color_to_add) ⇒ Object
Add a value to the hue component of a hsv color Ensures that the hue is still in the range 0..360
14 15 16 17 18 |
# File 'lib/color/hsv.rb', line 14 def self.add_hue_to_hsv(hue_value, hsv_color_to_add) hsv_color_to_add[0] += hue_value hsv_color_to_add[0] %= FULL_CIRCLE_DEG hsv_color_to_add end |
.calculate_hue(red, green, blue, max_min_diff) ⇒ Object
Method to calculate the hue value of three given rgb colors and the difference of the max - min color.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/color/hsv.rb', line 43 def self.calculate_hue(red, green, blue, max_min_diff) if red > green && red > blue HUE_MULTIPLIER * calculate_raw_hue_value(0, green, blue, max_min_diff) elsif green > red && green > blue HUE_MULTIPLIER * calculate_raw_hue_value(2, blue, red, max_min_diff) elsif blue > red && blue > green HUE_MULTIPLIER * calculate_raw_hue_value(4, red, green, max_min_diff) else 0 end end |
.calculate_monochromatic_color(hue_color) ⇒ Object
Calculate the a monochromatic value to a given color
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/color/hsv.rb', line 60 def self.calculate_monochromatic_color(hue_color) saturation = hue_color[1] value = hue_color[2] if saturation > 0.7 saturation -= MONO_SATURATION else saturation += MONO_SATURATION end value -= MONO_VALUE [hue_color[0], saturation.round(ROUND_VALUE), value] end |
.calculate_raw_hue_value(offset, first_color, second_color, max_min_diff) ⇒ Object
55 56 57 |
# File 'lib/color/hsv.rb', line 55 def self.calculate_raw_hue_value(offset, first_color, second_color, max_min_diff) offset + ((first_color - second_color).to_f / max_min_diff).round(ROUND_VALUE) end |
.calculate_saturation(max_min_diff, max, value) ⇒ Object
35 36 37 38 39 |
# File 'lib/color/hsv.rb', line 35 def self.calculate_saturation(max_min_diff, max, value) saturation = 0 saturation = (max_min_diff.to_f / max).round(ROUND_VALUE) if value.positive? saturation end |
.hsv_to_rgb(hue, saturation, value) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/color/hsv.rb', line 74 def self.hsv_to_rgb(hue, saturation, value) hue = hue.to_f / FULL_CIRCLE_DEG hue_i = (hue * 6).to_i f = (hue * 6) - hue_i p = value * (1 - saturation) q = value * (1 - (f * saturation)) t = value * (1 - ((1 - f) * saturation)) if hue_i.zero? red = value green = t blue = p end if hue_i == 1 red = q green = value blue = p end if hue_i == 2 red = p green = value blue = t end if hue_i == 3 red = p green = q blue = value end if hue_i == 4 red = t green = p blue = value end if hue_i == 5 red = value green = p blue = q end [(red * RGB_MAX_VALUE).round.abs, (green * RGB_MAX_VALUE).round.abs, (blue * RGB_MAX_VALUE).round.abs] end |
.rgb_to_hsv(rgb_color) ⇒ Object
Hex Colors can be represented with three characters, to work
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/color/hsv.rb', line 21 def self.rgb_to_hsv(rgb_color) max = rgb_color.max() min = rgb_color.min() r = rgb_color[0] g = rgb_color[1] b = rgb_color[2] value = (max.to_f / RGB_MAX_VALUE).round(ROUND_VALUE) max_min_diff = max - min hue = calculate_hue(r, g, b, max_min_diff) hue += FULL_CIRCLE_DEG if hue.negative? [hue.round, calculate_saturation(max_min_diff, max, value), value] end |