package org.deviceconnect.android.deviceplugin.alljoyn.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;

/**
 * Color utility.
 *
 * @author NTT DOCOMO, INC.
 */
public class ColorUtil {
    private ColorUtil() {

    }

    /**
     * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
     * precision, BigDecimal objects are used.
     *
     * @param rgb
     * @return
     */
    public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
        int[] hsb = new int[3];
        int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
        int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
        int diff = maxChroma - minChroma;

        // Hue
        BigDecimal hue;
        if (diff == 0) {
            hue = BigDecimal.ZERO;
        } else if (maxChroma == rgb[0]) {
            float tmp = (rgb[1] - rgb[2]) / (float) diff;
            if (tmp < 0) {
                tmp += 6 * Math.ceil(-tmp / 6.0);
            } else {
                tmp -= 6 * Math.floor(tmp / 6.0);
            }
            hue = BigDecimal.valueOf(60 * tmp);
        } else if (maxChroma == rgb[1]) {
            hue = BigDecimal.valueOf((int) (60 * ((rgb[2] - rgb[0]) / (float) diff + 2)));
        } else {
            hue = BigDecimal.valueOf((int) (60 * ((rgb[0] - rgb[1]) / (float) diff + 4)));
        }
        // [0, 360] -> [0, 0xffffffff]
        hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
        hue = hue.divide(BigDecimal.valueOf(360), RoundingMode.FLOOR);
        hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

        // Saturation
        if (maxChroma == 0) {
            hsb[1] = 0;
        } else {
            // [0, 1] -> [0, 0xffffffff]
            BigDecimal sat = BigDecimal.valueOf(diff);
            sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
            sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
            hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
        }

        // Brightness
        // [0, 255] -> [0, 0xffffffff]
        BigDecimal brightness = BigDecimal.valueOf(maxChroma);
        brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
        brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
        hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

        return hsb;
    }
}

@@ -1139,21 +1139,16 @@
 valueOf(
-60 * 
 tmp);%0A  
@@ -1219,37 +1219,24 @@
 imal.valueOf
-((int) (60 * 
 ((rgb%5B2%5D - r
@@ -1261,18 +1261,16 @@
 iff + 2)
-))
 ;%0A      
@@ -1320,21 +1320,8 @@
 ueOf
-((int) (60 * 
 ((rg
@@ -1354,18 +1354,16 @@
 iff + 4)
-))
 ;%0A      
@@ -1510,19 +1510,17 @@
 valueOf(
-360
+6
 ), Round

Optimize color space conversion calculation.