Module: PSD::Color
Overview
Various color conversion methods. All color values are stored in the PSD document in the color space as defined by the user instead of a normalized value of some kind. This means that we have to do all the conversion ourselves for each color space.
Instance Method Summary collapse
- #ahsb_to_color(alpha, hue, saturation, brightness) ⇒ Object
- #alab_to_color(alpha, l, a, b) ⇒ Object
- #argb_to_color(a, r, g, b) ⇒ Object
- #cmyk_to_color(c, m, y, k) ⇒ Object
- #cmyk_to_rgb(c, m, y, k) ⇒ Object
-
#color_space_to_argb(color_space, color_component) ⇒ Object
This is a relic of libpsd that will likely go away in a future version.
- #color_to_argb(color) ⇒ Object
- #hsb_to_color(*args) ⇒ Object
- #hue_to_color(hue, m1, m2) ⇒ Object
- #lab_to_color(*args) ⇒ Object
- #lab_to_xyz(l, a, b) ⇒ Object
- #rgb_to_color(*args) ⇒ Object
Instance Method Details
#ahsb_to_color(alpha, hue, saturation, brightness) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/psd/color.rb', line 52 def ahsb_to_color(alpha, hue, saturation, brightness) if saturation == 0.0 b = g = r = (255 * brightness).to_i else if brightness <= 0.5 m2 = brightness * (1 + saturation) else m2 = brightness + saturation - brightness * saturation end m1 = 2 * brightness - m2 r = hue_to_color(hue + 120, m1, m2) g = hue_to_color(hue, m1, m2) b = hue_to_color(hue - 120, m1, m2) end argb_to_color alpha, r, g, b end |
#alab_to_color(alpha, l, a, b) ⇒ Object
102 103 104 105 |
# File 'lib/psd/color.rb', line 102 def alab_to_color(alpha, l, a, b) xyz = lab_to_xyz(l, a, b) axyz_to_color alpha, xyz[:x], xyz[:y], xyz[:z] end |
#argb_to_color(a, r, g, b) ⇒ Object
44 45 46 |
# File 'lib/psd/color.rb', line 44 def argb_to_color(a, r, g, b) (a << 24) | (r << 16) | (g << 8) | b end |
#cmyk_to_color(c, m, y, k) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/psd/color.rb', line 86 def cmyk_to_color(c, m, y, k) r = 1 - (c * (1 - k) + k) * 255 g = 1 - (m * (1 - k) + k) * 255 b = 1 - (y * (1 - k) + k) * 255 r = [0, r, 255].sort[1] g = [0, g, 255].sort[1] b = [0, b, 255].sort[1] rgb_to_color r, g, b end |
#cmyk_to_rgb(c, m, y, k) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/psd/color.rb', line 117 def cmyk_to_rgb(c, m, y, k) Hash[{ r: (65535 - (c * (255 - k) + (k << 8))) >> 8, g: (65535 - (m * (255 - k) + (k << 8))) >> 8, b: (65535 - (y * (255 - k) + (k << 8))) >> 8 }.map { |k, v| [k, Util.clamp(v, 0, 255)] }] end |
#color_space_to_argb(color_space, color_component) ⇒ Object
This is a relic of libpsd that will likely go away in a future version. It stored the entire color value in a 32-bit address space for speed.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/psd/color.rb', line 11 def color_space_to_argb(color_space, color_component) color = case color_space when 0 rgb_to_color *color_component when 1 hsb_to_color color_component[0], color_component[1] / 100.0, color_component[2] / 100.0 when 2 cmyk_to_color color_component[0] / 100.0, color_component[1] / 100.0, color_component[2] / 100.0, color_component[3] / 100.0 when 7 lab_to_color *color_component else 0x00FFFFFF end color_to_argb(color) end |
#color_to_argb(color) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/psd/color.rb', line 31 def color_to_argb(color) [ (color) >> 24, ((color) & 0x00FF0000) >> 16, ((color) & 0x0000FF00) >> 8, (color) & 0x000000FF ] end |
#hsb_to_color(*args) ⇒ Object
48 49 50 |
# File 'lib/psd/color.rb', line 48 def hsb_to_color(*args) ahsb_to_color(255, *args) end |
#hue_to_color(hue, m1, m2) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/psd/color.rb', line 71 def hue_to_color(hue, m1, m2) hue = (hue % 360).to_i if hue < 60 v = m1 + (m2 - m1) * hue / 60 elsif hue < 180 v = m2 elsif hue < 240 v = m1 + (m2 - m1) * (240 - hue) / 60 else v = m1 end (v * 255).to_i end |
#lab_to_color(*args) ⇒ Object
98 99 100 |
# File 'lib/psd/color.rb', line 98 def lab_to_color(*args) alab_to_color(255, *args) end |
#lab_to_xyz(l, a, b) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/psd/color.rb', line 107 def lab_to_xyz(l, a, b) y = (l + 16) / 116 x = y + (a / 500) z = y - (b / 200) x, y, z = [x, y, z].map do |n| n**3 > 0.008856 ? n**3 : (n - 16 / 116) / 7.787 end end |
#rgb_to_color(*args) ⇒ Object
40 41 42 |
# File 'lib/psd/color.rb', line 40 def rgb_to_color(*args) argb_to_color(255, *args) end |