calculate Color from primary and secondary color and ratio - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

calculate Color from primary and secondary color and ratio

Demo Code


//package com.java2s;

public class Main {

    /**/* w  ww .ja  va 2 s  . c  o  m*/
     * calculateColor
     * @param primaryColor primaryColor
     * @param secondaryColor secondaryColor
     * @param ratio ratio
     * @return string
     */
    public static String calculateColor(String primaryColor,
            String secondaryColor, double ratio) {
        double primeRed = Integer
                .parseInt(primaryColor.substring(0, 2), 16);
        double primeGreen = Integer.parseInt(primaryColor.substring(2, 4),
                16);
        double primeBlue = Integer.parseInt(primaryColor.substring(4, 6),
                16);

        double secondRed = Integer.parseInt(secondaryColor.substring(0, 2),
                16);
        double secondGreen = Integer.parseInt(
                secondaryColor.substring(2, 4), 16);
        double secondBlue = Integer.parseInt(
                secondaryColor.substring(4, 6), 16);

        int newRed = (int) (secondRed * ratio + primeRed * (1 - ratio));
        int newGreen = (int) (secondGreen * ratio + primeGreen
                * (1 - ratio));
        int newBlue = (int) (secondBlue * ratio + primeBlue * (1 - ratio));

        return String.format("%06X", (newRed << 16) + (newGreen << 8)
                + newBlue);
    }
}

Related Tutorials