1
2
3
4
5
6
7 package com.octo.captcha.component.image.backgroundgenerator;
8
9 import com.octo.captcha.component.image.color.ColorGenerator;
10 import com.octo.captcha.component.image.color.SingleColorGenerator;
11
12 import java.awt.*;
13 import java.awt.image.BufferedImage;
14
15 /***
16 * <p/>
17 * Three color gradient background with randomization </p>
18 *
19 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a>
20 * @version 1.0
21 */
22 public class FunkyBackgroundGenerator extends AbstractBackgroundGenerator {
23 ColorGenerator colorGeneratorLeftUp = null;
24
25 ColorGenerator colorGeneratorLeftDown = null;
26
27 ColorGenerator colorGeneratorRightUp = null;
28
29 ColorGenerator colorGeneratorRightDown = null;
30
31 float perturbationlevel = 0.1f;
32
33 public FunkyBackgroundGenerator(Integer width, Integer height) {
34 this(width, height, new SingleColorGenerator(Color.yellow), new SingleColorGenerator(Color.red), new SingleColorGenerator(Color.yellow), new SingleColorGenerator(Color.green), 0.5f);
35 }
36
37 public FunkyBackgroundGenerator(Integer width, Integer height, ColorGenerator colorGenerator) {
38 this(width, height, colorGenerator, colorGenerator, colorGenerator, colorGenerator, 0.5f);
39 }
40
41 public FunkyBackgroundGenerator(Integer width, Integer height,
42 ColorGenerator colorGeneratorLeftUp, ColorGenerator colorGeneratorLeftDown,
43 ColorGenerator colorGeneratorRightUp, ColorGenerator colorGeneratorRightDown, float perturbationLevel) {
44 super(width, height);
45 this.colorGeneratorLeftUp = colorGeneratorLeftUp;
46 this.colorGeneratorLeftDown = colorGeneratorLeftDown;
47 this.colorGeneratorRightDown = colorGeneratorRightDown;
48 this.colorGeneratorRightUp = colorGeneratorRightUp;
49 this.perturbationlevel = perturbationLevel;
50 }
51
52 /***
53 * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
54 * imageWidth.
55 *
56 * @return the background image
57 */
58 public BufferedImage getBackground() {
59 Color colorLeftUp = colorGeneratorLeftUp.getNextColor();
60 Color colorLeftDown = colorGeneratorLeftDown.getNextColor();
61 Color colorRightUp = colorGeneratorRightUp.getNextColor();
62 Color colorRightDown = colorGeneratorRightDown.getNextColor();
63
64 BufferedImage bimgTP = new BufferedImage(getImageWidth(), getImageHeight(),
65 BufferedImage.TYPE_INT_BGR);
66 Graphics2D g2d = bimgTP.createGraphics();
67 g2d.setColor(Color.white);
68 g2d.fillRect(0, 0, getImageHeight(), getImageWidth());
69
70 float height = getImageHeight();
71 float width = getImageWidth();
72
73 for (int j = 0; j < getImageHeight(); j++) {
74 for (int i = 0; i < getImageWidth(); i++) {
75 float leftUpRatio = (1 - i / width) * (1 - j / height);
76 float leftDownRatio = (1 - i / width) * (j / height);
77 float rightUpRatio = (i / width) * (1 - j / height);
78 float rightDownRatio = (i / width) * (j / height);
79
80 float red = colorLeftUp.getRed() / 255.0f * leftUpRatio + colorLeftDown.getRed() / 255.0f
81 * leftDownRatio + colorRightUp.getRed() / 255.0f * rightUpRatio
82 + colorRightDown.getRed() / 255.0f * rightDownRatio;
83
84 float green = colorLeftUp.getGreen() / 255.0f * leftUpRatio + colorLeftDown.getGreen()
85 / 255.0f * leftDownRatio + colorRightUp.getGreen() / 255.0f * rightUpRatio
86 + colorRightDown.getGreen() / 255.0f * rightDownRatio;
87
88 float blue = colorLeftUp.getBlue() / 255.0f * leftUpRatio + colorLeftDown.getBlue()
89 / 255.0f * leftDownRatio + colorRightUp.getBlue() / 255.0f * rightUpRatio
90 + colorRightDown.getBlue() / 255.0f * rightDownRatio;
91
92 if (myRandom.nextFloat() > perturbationlevel)
93 g2d.setColor(new Color(red, green,
94 blue, 1.0f));
95 else
96 g2d.setColor(new Color(compute(red), compute(green),
97 compute(blue), 1.0f));
98 g2d.drawLine(i, j, i, j);
99 }
100 }
101 g2d.dispose();
102 return bimgTP;
103 }
104
105 private float compute(float f) {
106
107 float range = (1 - f < f) ? 1 - f : f;
108 if (myRandom.nextFloat() > 0.5f)
109 return f - myRandom.nextFloat() * range;
110 else
111 return f + myRandom.nextFloat() * range;
112 }
113
114 }