2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "color-adjuster.h"
26 const std::string HSVDELTA_PROPERTY_NAME("uHSVDelta");
28 ColorAdjuster::ColorAdjuster()
32 //Call the Parent copy constructor to add reference to the implementation for this object
33 ColorAdjuster::ColorAdjuster( ShaderEffect handle )
34 : ShaderEffect( handle )
38 ColorAdjuster::~ColorAdjuster()
42 ColorAdjuster ColorAdjuster::New( const Vector3& hsvDelta, bool ignoreAlpha )
44 std::string fragmentShader(
45 " precision highp float;\n"
46 " uniform vec3 uHSVDelta;\n"
47 " uniform float uIgnoreAlpha;\n"
48 " float rand(vec2 co) \n"
50 " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
52 " vec3 rgb2hsv(vec3 c)\n"
54 " vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"
55 " vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
56 " vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
58 " float d = q.x - min(q.w, q.y);\n"
59 " float e = 1.0e-10;\n"
60 " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
62 " vec3 hsv2rgb(vec3 c)\n"
64 " vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
65 " vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
66 " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
69 " vec4 color = texture2D(sTexture, vTexCoord); \n"
70 " vec3 hsvColor = rgb2hsv( color.rgb );\n"
71 // modify the hsv Value
72 " hsvColor += uHSVDelta * rand(vTexCoord); \n"
73 // if the new vale exceeds one, then decrease it
74 " hsvColor -= max(hsvColor*2.0 - vec3(2.0), 0.0);\n"
75 // if the new vale drops below zero, then increase it
76 " hsvColor -= min(hsvColor*2.0, 0.0);\n"
77 " color.rgb = hsv2rgb( hsvColor ); \n"
78 // uIgnoreAlpha decide the result alpha will be 1.0 or source's alpha
79 " color.a = clamp(color.a + uIgnoreAlpha, 0.0, 1.0);\n"
80 " gl_FragColor = color; \n"
83 ShaderEffect effect = ShaderEffect::New("", fragmentShader);
84 ColorAdjuster handle( effect );
85 handle.SetUniform( "uHSVDelta", hsvDelta );
86 handle.SetUniform( "uIgnoreAlpha", ignoreAlpha?1.0f:0.0f );
90 std::string ColorAdjuster::GetHsvDeltaPropertyName() const
92 return HSVDELTA_PROPERTY_NAME;
95 } // namespace Toolkit