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