package wordcloud.palette;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 * A LinearGradient ColorPalette
 * 
 * @author &#64;wolfposd
 */
public class LinearGradientColorPalette extends ColorPalette
{
    /**
     * <p>
     * Creates a ColorPalette using a linear gradient between the two colors
     * specified
     * </p>
     * <p>
     * The following example code will generate a gradient between red and blue,
     * with 18 colors in between red and blue, thus totaling 20 colors:
     * </p>
     * <code>
     * new LinearGradientColorPalette(Color.RED, 18, Color.Blue)
     * </code>
     * 
     * @param color1
     *            start color
     * @param betweenC1andC2
     *            specifies the amount of colors for this gradient between
     *            color1 and color2
     * @param color2
     *            end color
     */
    public LinearGradientColorPalette(Color color1, int betweenC1andC2, Color color2)
    {
        this.colors = createLinearGradient(color1, color2, betweenC1andC2).toArray(new Color[] {});
    }

    /**
     * Creates a ColorPalette using a linear gradient between the three colors
     * specified
     * 
     * @param color1
     *            start color
     * @param betweenC1andC2
     *            number of colors to be generated between color1 and color2
     * @param color2
     *            middle color
     * @param betweenC2andC3
     *            number of colors to be generated between color2 and color3
     * @param color3
     *            end color
     */
    public LinearGradientColorPalette(Color color1, int betweenC1andC2, Color color2, int betweenC2andC3, Color color3)
    {
        ArrayList<Color> cs = new ArrayList<>();

        for (Color c : createLinearGradient(color1, color2, betweenC1andC2))
        {
            cs.add(c);
        }

        for (Color c : createLinearGradient(color2, color3, betweenC2andC3))
        {
            if (c.equals(color2)) // already being added by first gradient
                continue;
            cs.add(c);
        }
        
        this.colors = cs.toArray(new Color[]{});
    }

    /**
     * Creates a linear Gradient between two colors
     * 
     * @param color1
     *            start color
     * @param color2
     *            end color
     * @param amountBetweenC1andC2
     *            specifies the amount of colors in this gradient between color1 and color2
     * @return List of colors in this gradient
     */
    public static List<Color> createLinearGradient(Color color1, Color color2, int amountBetweenC1andC2)
    {
        ArrayList<Color> cols = new ArrayList<>();

        for (int i = 0; i <= amountBetweenC1andC2; i++)
        {
            float ratio = (float) i / (float) amountBetweenC1andC2;

            int red = (int) (color2.getRed() * ratio + color1.getRed() * (1 - ratio));
            int green = (int) (color2.getGreen() * ratio + color1.getGreen() * (1 - ratio));
            int blue = (int) (color2.getBlue() * ratio + color1.getBlue() * (1 - ratio));

            cols.add(new Color(red, green, blue));
        }
        cols.add(color2);
        return cols;
    }
}

@@ -230,10 +230,11 @@
 ette
+ %7B
 %0A
-%7B
 %0A   
@@ -966,20 +966,16 @@
  color2)
-%0A   
  %7B%0A     
@@ -1679,20 +1679,16 @@
  color3)
-%0A   
  %7B%0A     
@@ -1682,37 +1682,32 @@
 lor3) %7B%0A        
-Array
 List%3CColor%3E cs =
@@ -1719,32 +1719,65 @@
 ArrayList%3C%3E();%0A%0A
+        // adding colors %5Bc1,c2%5D%0A
         for (Col
@@ -1836,24 +1836,16 @@
 1andC2))
-%0A       
  %7B%0A     
@@ -1868,24 +1868,65 @@
 ;%0A        %7D%0A
+        %0A        // adding colors %5Dc2,c3%5D
 %0A        for
@@ -1990,24 +1990,16 @@
 2andC3))
-%0A       
  %7B%0A     
@@ -2026,16 +2026,18 @@
 color2))
+ %7B
  // alre
@@ -2096,16 +2096,30 @@
 ntinue;%0A
+            %7D%0A
         
@@ -2139,32 +2139,24 @@
 ;%0A        %7D%0A
-        
 %0A        thi
@@ -2188,16 +2188,17 @@
  Color%5B%5D
+ 
 %7B%7D);%0A   
@@ -2476,32 +2476,50 @@
 t between color1
+%0A     *           
  and color2%0A    
@@ -2673,20 +2673,16 @@
 C1andC2)
-%0A   
  %7B%0A     
@@ -2684,21 +2684,16 @@
         
-Array
 List%3CCol
@@ -2778,24 +2778,16 @@
 C2; i++)
-%0A       
  %7B%0A     

change code formatting