Class: PSD::Color
- Inherits:
-
Object
- Object
- PSD::Color
- Defined in:
- lib/psd/color.rb
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.
Class 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
Class Method Details
.ahsb_to_color(alpha, hue, saturation, brightness) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/psd/color.rb', line 50 def self.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
100 101 102 103 |
# File 'lib/psd/color.rb', line 100 def self.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
42 43 44 |
# File 'lib/psd/color.rb', line 42 def self.argb_to_color(a, r, g, b) (a << 24) | (r << 16) | (g << 8) | b end |
.cmyk_to_color(c, m, y, k) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/psd/color.rb', line 84 def self.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
115 116 117 118 119 120 121 |
# File 'lib/psd/color.rb', line 115 def self.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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/psd/color.rb', line 9 def self.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
29 30 31 32 33 34 35 36 |
# File 'lib/psd/color.rb', line 29 def self.color_to_argb(color) [ (color) >> 24, ((color) & 0x00FF0000) >> 16, ((color) & 0x0000FF00) >> 8, (color) & 0x000000FF ] end |
.hsb_to_color(*args) ⇒ Object
46 47 48 |
# File 'lib/psd/color.rb', line 46 def self.hsb_to_color(*args) ahsb_to_color(255, *args) end |
.hue_to_color(hue, m1, m2) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/psd/color.rb', line 69 def self.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
96 97 98 |
# File 'lib/psd/color.rb', line 96 def self.lab_to_color(*args) alab_to_color(255, *args) end |
.lab_to_xyz(l, a, b) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/psd/color.rb', line 105 def self.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
38 39 40 |
# File 'lib/psd/color.rb', line 38 def self.rgb_to_color(*args) argb_to_color(255, *args) end |