-
Notifications
You must be signed in to change notification settings - Fork 89
/
TintEffect.java
64 lines (53 loc) · 2.28 KB
/
TintEffect.java
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
package com.sherazkhilji.videffects;
import android.graphics.Color;
import android.opengl.GLSurfaceView;
import com.sherazkhilji.videffects.interfaces.ShaderInterface;
/**
* Tints the video with specified color..
*
* @author sheraz.khilji
*/
public class TintEffect implements ShaderInterface {
private int mTint = 0xFF0000FF;
/**
* Initialize Effect
*
* @param color Integer, representing an ARGB color with 8 bits per channel.
* May be created using Color class.
*/
public TintEffect(int color) {
this.mTint = color;
}
@Override
public String getShader(GLSurfaceView mGlSurfaceView) {
float[] color_ratio = {0.21f, 0.71f, 0.07f};
String[] color_ratioString = new String[3];
color_ratioString[0] = "color_ratio[0] = " + color_ratio[0] + ";\n";
color_ratioString[1] = "color_ratio[1] = " + color_ratio[1] + ";\n";
color_ratioString[2] = "color_ratio[2] = " + color_ratio[2] + ";\n";
float[] tint_color = {Color.red(mTint) / 255f,
Color.green(mTint) / 255f, Color.blue(mTint) / 255f};
String[] tintString = new String[3];
tintString[0] = "tint[0] = " + tint_color[0] + ";\n";
tintString[1] = "tint[1] = " + tint_color[1] + ";\n";
tintString[2] = "tint[2] = " + tint_color[2] + ";\n";
return "#extension GL_OES_EGL_image_external : require\n"
+ "precision mediump float;\n"
+ "uniform samplerExternalOES sTexture;\n"
+ " vec3 tint;\n"
+ " vec3 color_ratio;\n"
+ "varying vec2 vTextureCoord;\n"
+ "void main() {\n"
// Parameters that were created above
+ color_ratioString[0]
+ color_ratioString[1]
+ color_ratioString[2]
+ tintString[0]
+ tintString[1]
+ tintString[2]
+ " vec4 color = texture2D(sTexture, vTextureCoord);\n"
+ " float avg_color = dot(color_ratio, color.rgb);\n"
+ " vec3 new_color = min(0.8 * avg_color + 0.2 * tint, 1.0);\n"
+ " gl_FragColor = vec4(new_color.rgb, color.a);\n" + "}\n";
}
}