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,895 questions

51,826 answers

573 users

How to convert RGB to CMYK in Java

1 Answer

0 votes
// CMYK = Cyan, Magenta, Yellow, Key(black)
// RGB = Red, Green, Blue
 
import java.util.Arrays;
 
public class MyClass {
    private static double[] RGBtoCMYK(double R, double G, double B) {
        if (R == 0 && G == 0 && B == 0) {
            return new double[]{ 0, 0, 0, 1 };
        }
 
        R = R / 255.0 * 100;
        G = G / 255.0 * 100;
        B = B / 255.0 * 100;
 
        double K = 100 - Math.max(Math.max((int)R, (int)G), (int)B);
 
        if (K == 100) {
            return new double[]{ 0, 0, 0, 100 };
        }
 
        double C = Math.round((100 - R - K) / (100 - K) * 100);
        double M = Math.round((100 - G - K) / (100 - K) * 100);
        double Y = Math.round((100 - B - K) / (100 - K) * 100);
 
        return new double[]{ C, M, Y, K };
    }
    public static void main(String args[]) {
        double[] CMYK  = RGBtoCMYK(245.0f, 213.0f, 0.0f);
 
        System.out.println(Arrays.toString(CMYK));
    }
}
 
 
 
 
/*
run:
 
[0.0, 13.0, 100.0, 4.0]
 
*/

 



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

Related questions

1 answer 90 views
90 views asked Jan 30, 2023 by avibootz
1 answer 122 views
122 views asked Feb 2, 2023 by avibootz
1 answer 124 views
124 views asked Feb 1, 2023 by avibootz
1 answer 121 views
121 views asked Feb 1, 2023 by avibootz
1 answer 138 views
138 views asked Jan 31, 2023 by avibootz
1 answer 96 views
96 views asked Jan 31, 2023 by avibootz
1 answer 116 views
116 views asked Jan 31, 2023 by avibootz
...