1

I am trying to use 4 UISliders to change the values of the CMYK and then display the color in a view but I have tried with different formulas and still I don't get the right values, So I found this answer in stack overflow but is for android (Java), but I don't get the code, this is what I have written:

let cyan = Float( 1 - (cyanSlider.value / 255))
let magenta = Float(1 - (magentaSlider.value / 255))
let yellow = Float(1 - (yellowSlider.value / 255))

cyanValue.text = String(stringInterpolationSegment: cyan)
magentaValue.text = String(stringInterpolationSegment: magenta)
yellowValue.text = String(stringInterpolationSegment: yellow) 

And in the answer is:

int r,g,b,c,m,y,k;
int computedC,computedM,computedY;
int minCMY;

if(r==0 && g==0 && b==0) return {0,0,0,1}

computedC = 1 - (r/255);
computedM = 1 - (g/255);
computedY = 1 - (b/255);

minCMY = Math.min(computedC,Math.min(computedM,computedY));

computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;

return {computedC,computedM,computedY,minCMY};

I don't get this part of the code:

 minCMY = Math.min(computedC,Math.min(computedM,computedY));

How would I write this on swift? or maybe objective -c and I will try to understand the objective-c code, but preferably swift please.

Thanks, I am very new to swift :(

Community
  • 1
  • 1
Pixele9
  • 422
  • 8
  • 25

1 Answers1

4

Swift has a "built-in" min() function which takes a variable number of arguments, so your code can easily be translated to Swift as

func RGBtoCMYK(r : CGFloat, g : CGFloat, b : CGFloat) -> (c : CGFloat, m : CGFloat, y : CGFloat, k : CGFloat) {

    if r==0 && g==0 && b==0 {
        return (0, 0, 0, 1)
    }
    var c = 1 - r
    var m = 1 - g
    var y = 1 - b
    let minCMY = min(c, m, y)
    c = (c - minCMY) / (1 - minCMY)
    m = (m - minCMY) / (1 - minCMY)
    y = (y - minCMY) / (1 - minCMY)
    return (c, m, y, minCMY)
}

Note that I have modified the function to take RGB color components as CGFloat in the range 0.0 ... 1.0, which is the usual representation in UIKit classes such as UIColor.

But what you actually need is the reverse conversion from CMYK to RGB:

// From http://www.rapidtables.com/convert/color/cmyk-to-rgb.htm
func CMYKtoRGB(c : CGFloat, m : CGFloat, y : CGFloat, k : CGFloat) -> (r : CGFloat, g : CGFloat, b : CGFloat) {
    let r = (1 - c) * (1 - k)
    let g = (1 - m) * (1 - k)
    let b = (1 - y) * (1 - k)
    return (r, g, b)
}

which you can use to set the background color of a view from the CMYK slider values:

let c = CGFloat(cyanSlider.value)
let m = CGFloat(magentaSlider.value)
let y = CGFloat(yellowSlider.value)
let k = CGFloat(blackSlider.value)
let (r, g, b) = CMYKtoRGB(c, m, y, k)
rgbView.backgroundColor = UIColor(red: r, green: g, blue: b, alpha: 1.0)

(Note that the above conversion methods are very simple. For better results one would use ICC-based color conversion, see for example UIColor CMYK and Lab Values?.)

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks I will try that!!! But How can I make that if i just have a view controller with 4 sliders and a view to display the color, so how or where can I get the RGB values? – Pixele9 Jul 28 '15 at 05:26
  • @Pixele9: Do you mean 4 sliders for the CMYK values? – Martin R Jul 28 '15 at 05:28
  • Well it's a slider for each color value, so it is: one slider for C then other slider for M, and like that – Pixele9 Jul 28 '15 at 05:31
  • @Pixele9: So you need to convert the CMYK values from the sliders to RGB values for the view. That's why I asked which conversion direction you need. – Martin R Jul 28 '15 at 05:32
  • oooh sorry, so how would it be?? – Pixele9 Jul 28 '15 at 05:33
  • Thank! how could I make it to have 2 decimals as in the web page you added?? http://www.rapidtables.com/convert/color/cmyk-to-rgb.htm – Pixele9 Jul 28 '15 at 15:46
  • 1
    @Pixele9: If you are talking about *displaying* the numbers (with 2 decimals) then have a look at NSNumberFormatter. – Martin R Jul 28 '15 at 16:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84493/discussion-between-pixele9-and-martin-r). – Pixele9 Jul 28 '15 at 17:13
  • Are these methods really useful and correct? RGB HSA XYZ LAB use the additive method and XYZ LAB use the coordinates of the ICC color profile (srgb, displayP3, etc ...). CMYK uses the subtractive method and a conversion without color profile creates non-real colors. For example, macOS color picker does not do conversions without a color profile and there are many reason. I'd like to hear an opinion about it. https://lea.verou.me/2009/03/100-cyan-in-cmyk-is-not-rgb0255255/ – Joannes Sep 23 '21 at 15:44
  • @Joannes: You are completely right. This is the “simple” DeviceRGB to DeviceCMYK to conversation, as e.g. described in the Adobe PostScript Language Reference. For color correct results you would do a conversion with ICC profiles. – Martin R Sep 23 '21 at 17:34
  • @MartinR thank you very much for the reply. If the correct conversion from RGB (like AdobeRGB) to CMYK (like frogra39) using ICC profiles is possible without too many problems, I found it impossible to return to the initial RGB profile with the same values. CMYK profiles are much smaller than RGB profiles and rounding creates big problems. Do you agree? – Joannes Sep 24 '21 at 11:55