B4J Question Inline Java Error - RGB to CMYK

IndieDev

Active Member
Licensed User
Hi,

Wanted to convert RGB to CMYK values.

Trying to use the following Java code, in #If Java statement.
Java:
#If Java
    import java.util.Arrays;

    public class RgbCmykConverter {
        //static
        public  void main(String[] args) {
            int[] cmyk = rgbToCmyk(
                Integer.parseInt(args[0]),
                Integer.parseInt(args[1]),
                Integer.parseInt(args[2])
            );

            System.out.println(Arrays.toString(cmyk));
        }
        //static
        private  int[] rgbToCmyk(int r, int g, int b) {
            double percentageR = r / 255.0 * 100;
            double percentageG = g / 255.0 * 100;
            double percentageB = b / 255.0 * 100;

            double k = 100 - Math.max(Math.max(percentageR, percentageG), percentageB);

            if (k == 100) {
                return new int[]{ 0, 0, 0, 100 };
            }

            int c = (int)((100 - percentageR - k) / (100 - k) * 100);
            int m = (int)((100 - percentageG - k) / (100 - k) * 100);
            int y = (int)((100 - percentageB - k) / (100 - k) * 100);

            return new int[]{ c, m, y, (int)k };
        }
    }
#End If

Calling the Java Inline code like this:
B4X:
    cmyk = asJO(Me).RunMethod("RgbCmykConverter", Array(argb(1), argb(2), argb(3)))

The variable "cmyk" is declared thus.
B4X:
    Private cmyk(4) As Float

This is the asJO function.
B4X:
Sub asJO(jo As JavaObject)As JavaObject
    Return jo
End Sub

Keep getting errors.
main._t_tick (java line: 506)
java.lang.RuntimeException: Method: RgbCmykConverter not found in: b4j.example.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
at b4j.example.main._t_tick(main.java:506)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

What am I doing wrong here?

Regards.
 

stevel05

Expert
Licensed User
Longtime User
A couple of problems I can see.

You don't need to create a class, you can call the sub directly as a public static method:

B4X:
#If java
         public  static int[] rgbToCmyk(int r, int g, int b) {
            double percentageR = r / 255.0 * 100;
            double percentageG = g / 255.0 * 100;
            double percentageB = b / 255.0 * 100;

            double k = 100 - Math.max(Math.max(percentageR, percentageG), percentageB);

            if (k == 100) {
                return new int[]{ 0, 0, 0, 100 };
            }

            int c = (int)((100 - percentageR - k) / (100 - k) * 100);
            int m = (int)((100 - percentageG - k) / (100 - k) * 100);
            int y = (int)((100 - percentageB - k) / (100 - k) * 100);

            return new int[]{ c, m, y, (int)k };
        }
#End If

Call as:

B4X:
    Dim cmyk() As Int = Jo.RunMethod("rgbToCmyk", Array(121,85,120))
    For i = 0 To 3
        Log(cmyk(i))
    Next

The sub returns an Int array, you are trying to assign it to a float array which will fail.

This is an example where it would be easier to convert it to B4x code.
 
Last edited:
Upvote 0

IndieDev

Active Member
Licensed User
Thank you, stevel05.
Will check with your updated code and revert.

This is an example where it would be easier to convert it to B4x code.
Will try to, else will ask for help here. :)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
B4X:
Public Sub RGb2CMYK(R As Int, G As Int, B As Int) As Int()
    
    Dim PercentageR As Double = R / 255 * 100
    Dim PercentageG As Double = G / 255 * 100
    Dim PercentageB As Double = B / 255 * 100
    
    Dim K As Double = 100 - Max(Max(PercentageR,PercentageG),PercentageB)
    
    If k = 100 Then Return Array As Int(0,0,0,100)
    
    Dim C As Int = ((100 - PercentageR - K) / (100 - K) * 100)
    Dim M As Int = ((100 - PercentageG - K) / (100 - K) * 100)
    Dim Y As Int = ((100 - PercentageB - K) / (100 - K) * 100)
    
    Return Array As Int(C,M,Y,K)
    
End Sub
 
Upvote 0

IndieDev

Active Member
Licensed User
B4X:
Public Sub RGb2CMYK(R As Int, G As Int, B As Int) As Int()
   
    Dim PercentageR As Double = R / 255 * 100
    Dim PercentageG As Double = G / 255 * 100
    Dim PercentageB As Double = B / 255 * 100
   
    Dim K As Double = 100 - Max(Max(PercentageR,PercentageG),PercentageB)
   
    If k = 100 Then Return Array As Int(0,0,0,100)
   
    Dim C As Int = ((100 - PercentageR - K) / (100 - K) * 100)
    Dim M As Int = ((100 - PercentageG - K) / (100 - K) * 100)
    Dim Y As Int = ((100 - PercentageB - K) / (100 - K) * 100)
   
    Return Array As Int(C,M,Y,K)
   
End Sub

Perfect!!!

Thank you, stevel05. :) šŸ™
 
Upvote 0
Top