Merge "Remove obsolete and non functional SizeChanged signal from actor" into tizen
[platform/core/uifw/dali-toolkit.git] / 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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 // CLASS HEADER
19 #include "color-adjuster.h"
20
21 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace
28 {
29 const std::string HSVDELTA_PROPERTY_NAME("uHSVDelta");
30 }
31
32 ColorAdjuster::ColorAdjuster()
33 {
34 }
35
36 //Call the Parent copy constructor to add reference to the implementation for this object
37 ColorAdjuster::ColorAdjuster( ShaderEffect handle )
38 : ShaderEffect( handle )
39 {
40 }
41
42 ColorAdjuster::~ColorAdjuster()
43 {
44 }
45
46 ColorAdjuster ColorAdjuster::New( const Vector3& hsvDelta, bool ignoreAlpha )
47 {
48   std::string fragmentShader(
49   "  precision highp float;\n"
50   "  uniform vec3 uHSVDelta;\n"
51   "  uniform float uIgnoreAlpha;\n"
52   "  float rand(vec2 co) \n"
53   "  {\n"
54   "    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
55   "  }\n"
56   "  vec3 rgb2hsv(vec3 c)\n"
57   "  {\n"
58   "    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"
59   "    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
60   "    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
61   "    \n"
62   "    float d = q.x - min(q.w, q.y);\n"
63   "    float e = 1.0e-10;\n"
64   "    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
65   "  }\n"
66   "  vec3 hsv2rgb(vec3 c)\n"
67   "  {\n"
68   "   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
69   "   vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
70   "   return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
71   "  }\n"
72   "  void main() {\n"
73   "    vec4 color = texture2D(sTexture, vTexCoord); \n"
74   "    vec3 hsvColor = rgb2hsv( color.rgb );\n"
75   // modify the hsv Value
76   "    hsvColor += uHSVDelta * rand(vTexCoord); \n"
77   // if the new vale exceeds one, then decrease it
78   "    hsvColor -= max(hsvColor*2.0 - vec3(2.0), 0.0);\n"
79   // if the new vale drops below zero, then increase it
80   "    hsvColor -= min(hsvColor*2.0, 0.0);\n"
81   "    color.rgb = hsv2rgb( hsvColor ); \n"
82   // uIgnoreAlpha decide the result alpha will be 1.0 or source's alpha
83   "    color.a = clamp(color.a + uIgnoreAlpha, 0.0, 1.0);\n"
84   "    gl_FragColor = color; \n"
85   "  }\n");
86
87   ShaderEffect effect = ShaderEffect::New("", fragmentShader);
88   ColorAdjuster handle( effect );
89   handle.SetUniform( "uHSVDelta", hsvDelta );
90   handle.SetUniform( "uIgnoreAlpha", ignoreAlpha?1.0f:0.0f );
91   return handle;
92 }
93
94 std::string ColorAdjuster::GetHsvDeltaPropertyName() const
95 {
96   return HSVDELTA_PROPERTY_NAME;
97 }
98
99 } // namespace Toolkit
100
101 } // namespace Dali