forked from daizhenjun/ImageFilterForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradient.java
More file actions
185 lines (162 loc) · 6.11 KB
/
Copy pathGradient.java
File metadata and controls
185 lines (162 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* HaoRan ImageFilter Classes v0.1
* Copyright (C) 2012 Zhenjun Dai
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation.
*/
package HaoRan.ImageFilter;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
public class Gradient {
public List<Integer> MapColors;
public Gradient()
{
List<Integer> list = new ArrayList<Integer>();
list.add(Color.BLACK);
list.add(Color.WHITE);
this.MapColors = list;
}
public Gradient(List<Integer> colors)
{
this.MapColors = colors;
}
private Palette CreateGradient(List<Integer> colors, int length)
{
if (colors == null || colors.size() < 2){
return null;
}
Palette palette = new Palette(length);
int[] red = palette.Red;
int[] green = palette.Green;
int[] blue = palette.Blue;
int num = length / (colors.size() - 1);
float num1 = 1f / ((float)num);
int index = 0;
int rgb = colors.get(0);
int colorR = (rgb & 0x00FF0000) >> 16;
int colorG = (rgb & 0x0000FF00) >> 8;
int colorB = rgb & 0x000000FF;
for (int i = 1; i < colors.size(); i++){
int r = (colors.get(i) & 0x00FF0000) >> 16;
int g = (colors.get(i) & 0x0000FF00) >> 8;
int b = colors.get(i) & 0x000000FF;
for (int j = 0; j < num; j++) {
float num2 = j * num1;
int rr = colorR + ((int)((r - colorR) * num2));
int gg = colorG + ((int)((g - colorG) * num2));
int bb = colorB + ((int)((b - colorB) * num2));
red[index] = (rr > 0xff ? 0xff : ((rr < 0) ? 0 : rr));
green[index]= (gg > 0xff ? 0xff : ((gg < 0) ? 0 : gg));
blue[index] = (bb > 0xff ? 0xff : ((bb < 0) ? 0 : bb));
index++;
}
colorR = r;
colorG = g;
colorB = b;
}
if (index < length) {
red[index] = red[index - 1];
green[index] = green[index - 1];
blue[index] = blue[index - 1];
}
return palette;
}
public Palette CreatePalette(int length)
{
return CreateGradient(this.MapColors, length);
}
public static class TintColors
{
public static int LightCyan() {
return (0xff << 24) + (0xeb << 16) + (0xf5 << 8) + 0xe1;
}
public static int Sepia(){
return (0xff << 24) + (179 << 16) + (179 << 8) + 230;
}
}
public static Gradient BlackSepia()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.BLACK);
colors.add(TintColors.Sepia());
return new Gradient(colors);
}
public static Gradient WhiteSepia()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.WHITE);
colors.add(TintColors.Sepia());
return new Gradient(colors);
}
public static Gradient RainBow()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.RED);
colors.add(Color.MAGENTA);
colors.add(Color.BLUE);
colors.add(Color.CYAN);
colors.add(Color.GREEN);
colors.add(Color.YELLOW);
colors.add(Color.RED);
return new Gradient(colors);
}
public static Gradient Inverse()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.WHITE);
colors.add(Color.BLACK);
return new Gradient(colors);
}
public static Gradient Fade()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.BLACK);
colors.add(Color.rgb(0xCD, 0xE8, 0xEE));//Cornsilk2 , reference http://www.wescn.com/tool/color_3.html
colors.add(Color.BLACK);
return new Gradient(colors);
}
public static Gradient Scene()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.rgb(0x00, 0xD7, 0xFF));//Gold , reference http://www.wescn.com/tool/color_3.html
colors.add(Color.WHITE);
colors.add(Color.rgb(0x00, 0xD7, 0xFF));//Gold
return new Gradient(colors);
}
public static Gradient Scene1()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.rgb(0xED, 0x95, 0x64));//CornflowerBlue , reference http://www.wescn.com/tool/color_3.html
colors.add(Color.WHITE);
colors.add(Color.rgb(0xED, 0x95, 0x64));//CornflowerBlue
return new Gradient(colors);
}
public static Gradient Scene2()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.rgb(0xFF, 0xBF, 0x00));//DeepSkyBlue , reference http://www.wescn.com/tool/color_3.html
colors.add(Color.WHITE);
colors.add(Color.rgb(0xFF, 0xBF, 0x00));//DeepSkyBlue
return new Gradient(colors);
}
public static Gradient Scene3()
{
List<Integer> colors = new ArrayList<Integer>();
colors.add(Color.rgb(0x00, 0xa5, 0xff));// , reference http://www.wescn.com/tool/color_3.html
colors.add(Color.WHITE);
colors.add(Color.rgb(0x00, 0xa5, 0xff));//
return new Gradient(colors);
}
}