Class: Abachrome::ColorModels::CMYK

Inherits:
Object
  • Object
show all
Includes:
ToAbcd
Defined in:
lib/abachrome/color_models/cmyk.rb

Class Method Summary collapse

Methods included from ToAbcd

#to_abcd

Class Method Details

.from_rgb_gcr(r, g, b, amount = 1.0) ⇒ Array<AbcDecimal>

Alias for from_rgb_ucr with full GCR (the standard approach). GCR is essentially the same as UCR but allows control over how much of the gray component to extract.

Parameters:

  • r (Numeric)

    Red component (0-1)

  • g (Numeric)

    Green component (0-1)

  • b (Numeric)

    Blue component (0-1)

  • amount (Numeric) (defaults to: 1.0)

    Amount of GCR to apply (0-1), default 1.0

Returns:

  • (Array<AbcDecimal>)

    Array of [c, m, y, k] values



118
119
120
# File 'lib/abachrome/color_models/cmyk.rb', line 118

def from_rgb_gcr(r, g, b, amount = 1.0)
  from_rgb_ucr(r, g, b, amount)
end

.from_rgb_naive(r, g, b) ⇒ Array<AbcDecimal>

Converts RGB values to CMYK using the standard naive conversion. This produces high ink coverage and is generally not recommended for production.

Parameters:

  • r (Numeric)

    Red component (0-1)

  • g (Numeric)

    Green component (0-1)

  • b (Numeric)

    Blue component (0-1)

Returns:

  • (Array<AbcDecimal>)

    Array of [c, m, y, k] values



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/abachrome/color_models/cmyk.rb', line 63

def from_rgb_naive(r, g, b)
  r = AbcDecimal(r)
  g = AbcDecimal(g)
  b = AbcDecimal(b)

  # Simple complementary conversion
  c = AbcDecimal(1) - r
  m = AbcDecimal(1) - g
  y = AbcDecimal(1) - b
  k = AbcDecimal(0)

  [c, m, y, k]
end

.from_rgb_ucr(r, g, b, gcr_amount = 1.0) ⇒ Array<AbcDecimal>

Converts RGB values to CMYK using Undercolor Removal (UCR). This extracts the gray component to the black (K) channel, reducing ink usage.

Parameters:

  • r (Numeric)

    Red component (0-1)

  • g (Numeric)

    Green component (0-1)

  • b (Numeric)

    Blue component (0-1)

  • gcr_amount (Numeric) (defaults to: 1.0)

    Gray Component Replacement amount (0-1), default 1.0 for full UCR

Returns:

  • (Array<AbcDecimal>)

    Array of [c, m, y, k] values



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/abachrome/color_models/cmyk.rb', line 85

def from_rgb_ucr(r, g, b, gcr_amount = 1.0)
  r = AbcDecimal(r)
  g = AbcDecimal(g)
  b = AbcDecimal(b)
  gcr = AbcDecimal(gcr_amount)

  # Calculate complementary CMY values
  c = AbcDecimal(1) - r
  m = AbcDecimal(1) - g
  y = AbcDecimal(1) - b

  # Find the minimum (gray component)
  k = [c, m, y].min * gcr

  # Remove the gray component from CMY if k > 0
  if k.positive?
    c -= k
    m -= k
    y -= k
  end

  [c, m, y, k]
end

.normalize(c, m, y, k) ⇒ Array<AbcDecimal>

Normalizes CMYK color component values to the [0,1] range.

Parameters:

  • c (Numeric)

    Cyan component (0-1 or 0-100 for percentages)

  • m (Numeric)

    Magenta component (0-1 or 0-100 for percentages)

  • y (Numeric)

    Yellow component (0-1 or 0-100 for percentages)

  • k (Numeric)

    Key/Black component (0-1 or 0-100 for percentages)

Returns:

  • (Array<AbcDecimal>)

    Array of four normalized components as AbcDecimal objects



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/abachrome/color_models/cmyk.rb', line 36

def normalize(c, m, y, k)
  [c, m, y, k].map do |value|
    case value
    when String
      if value.end_with?("%")
        value_without_percent = value.chomp("%")
        AbcDecimal(value_without_percent) / AbcDecimal(100)
      else
        AbcDecimal(value)
      end
    when Numeric
      if value > 1
        AbcDecimal(value) / AbcDecimal(100)
      else
        AbcDecimal(value)
      end
    end
  end
end

.to_rgb(c, m, y, k) ⇒ Array<AbcDecimal>

Converts CMYK values back to RGB. This is a straightforward inverse of the naive RGB→CMY conversion plus the addition of the black channel.

Parameters:

  • c (Numeric)

    Cyan component (0-1)

  • m (Numeric)

    Magenta component (0-1)

  • y (Numeric)

    Yellow component (0-1)

  • k (Numeric)

    Key/Black component (0-1)

Returns:

  • (Array<AbcDecimal>)

    Array of [r, g, b] values



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/abachrome/color_models/cmyk.rb', line 131

def to_rgb(c, m, y, k)
  c = AbcDecimal(c)
  m = AbcDecimal(m)
  y = AbcDecimal(y)
  k = AbcDecimal(k)

  # Standard CMYK to RGB conversion
  r = (AbcDecimal(1) - c) * (AbcDecimal(1) - k)
  g = (AbcDecimal(1) - m) * (AbcDecimal(1) - k)
  b = (AbcDecimal(1) - y) * (AbcDecimal(1) - k)

  [r, g, b]
end

.total_area_coverage(c, m, y, k) ⇒ AbcDecimal

Calculates the Total Area Coverage (TAC) for CMYK values. TAC is the sum of all four ink percentages and is critical in print workflows to prevent excessive ink that can cause smearing or paper damage.

Parameters:

  • c (Numeric)

    Cyan component (0-1)

  • m (Numeric)

    Magenta component (0-1)

  • y (Numeric)

    Yellow component (0-1)

  • k (Numeric)

    Key/Black component (0-1)

Returns:

  • (AbcDecimal)

    Total ink coverage as a decimal (0-4, often expressed as 0-400%)



154
155
156
# File 'lib/abachrome/color_models/cmyk.rb', line 154

def total_area_coverage(c, m, y, k)
  AbcDecimal(c) + AbcDecimal(m) + AbcDecimal(y) + AbcDecimal(k)
end