Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,892 questions

51,823 answers

573 users

How to convert RGB to CMYK in C++

1 Answer

0 votes
// CMYK = Cyan, Magenta, Yellow, Key(black)
// RGB = Red, Green, Blue
  
#include <iostream>
#include <cmath>

struct CMYK {
    float C;
    float M;
    float Y;
    float K;
};

struct CMYK RGBtoCMYK(float R, float G, float B) {
    struct CMYK cmyk;
 
    if (R == 0 && G == 0 && B == 0) {
        cmyk.C = 0;
        cmyk.M = 0;
        cmyk.Y = 0;
        cmyk.K = 1;
 
        return cmyk;
    }
 
    R = R / 255;
    G = G / 255;
    B = B / 255;
 
    float max = R;
 
    if (max < G)
        max = G;
    if (max < B)
        max = B;
 
    float white = max;
 
    cmyk.C = ((white - R) / white) * 100;
    cmyk.M = ((white - G) / white) * 100;
    cmyk.Y = ((white - B) / white) * 100;
    cmyk.K = (1.0f - white) * 100;
    
    return cmyk;
 
}

int main() {
    struct CMYK cmyk = RGBtoCMYK(245.0f, 213.0f, 0.0f);

    std::cout << "Cyan: " << round(cmyk.C) << "\n";
    std::cout << "Magenta: " << round(cmyk.M) << "\n";
    std::cout << "Yellow: " << round(cmyk.Y) << "\n";
    std::cout << "Black: " <<  round(cmyk.K) << "\n";
}



  
  
/*
run:
  
Cyan: 0
Magenta: 13
Yellow: 100
Black: 4

*/

 



answered Jan 31, 2023 by avibootz
edited Jan 31, 2023 by avibootz

Related questions

1 answer 89 views
89 views asked Jan 30, 2023 by avibootz
1 answer 95 views
95 views asked Jan 31, 2023 by avibootz
1 answer 85 views
85 views asked Jan 30, 2023 by avibootz
1 answer 122 views
122 views asked Feb 2, 2023 by avibootz
1 answer 123 views
123 views asked Feb 1, 2023 by avibootz
1 answer 121 views
121 views asked Feb 1, 2023 by avibootz
1 answer 125 views
125 views asked Jan 31, 2023 by avibootz
...