Search code examples
javacastingintegerrgbraytracing

Cast from double to int without truncating to zero


I'm creating a Ray Tracer and I'm finding the ambient, diffuse, and specular intensity of my shapes(spheres). When I try to cast these values that are less than 1 to an integer value they get truncated back to zero.

int red = Color.getRed() * (int)totalIntensity;
        int green = Color.getGreen() * (int)totalIntensity;
        int blue = Color.getBlue() * (int)totalIntensity;
return new Color(red, green, blue);

The problem is that I need the values of red, green, and blue to be integers so I can return the new color. Is there anyway I can do this without turning all of the totalIntensity values to zero?


Solution

  • I believe that int red = (int) (Color.getRed() * totalIntensity); should do the job.