Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / bubble-effect / color-adjuster.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 #include "color-adjuster.h"
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24 namespace
25 {
26   const std::string HSVDELTA_PROPERTY_NAME("uHSVDelta");
27 }
28 ColorAdjuster::ColorAdjuster()
29 {
30 }
31
32 //Call the Parent copy constructor to add reference to the implementation for this object
33 ColorAdjuster::ColorAdjuster( ShaderEffect handle )
34 : ShaderEffect( handle )
35 {
36 }
37
38 ColorAdjuster::~ColorAdjuster()
39 {
40 }
41
42 ColorAdjuster ColorAdjuster::New( const Vector3& hsvDelta, bool ignoreAlpha )
43 {
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"
49   "  {\n"
50   "    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
51   "  }\n"
52   "  vec3 rgb2hsv(vec3 c)\n"
53   "  {\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"
57   "    \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"
61   "  }\n"
62   "  vec3 hsv2rgb(vec3 c)\n"
63   "  {\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"
67   "  }\n"
68   "  void main() {\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"
81   "  }\n");
82
83   ShaderEffect effect = ShaderEffect::New("", fragmentShader);
84   ColorAdjuster handle( effect );
85   handle.SetUniform( "uHSVDelta", hsvDelta );
86   handle.SetUniform( "uIgnoreAlpha", ignoreAlpha?1.0f:0.0f );
87   return handle;
88 }
89
90 std::string ColorAdjuster::GetHsvDeltaPropertyName() const
91 {
92   return HSVDELTA_PROPERTY_NAME;
93 }
94
95 } // namespace Toolkit
96
97 } // namespace Dali