Merge "Fixed testcases using sampler uniforms" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bubble-effect / color-adjuster.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H_
2 #define __DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H_
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20 #include <dali/public-api/math/vector3.h>
21 #include <dali/public-api/object/property-map.h>
22
23 #define DALI_COMPOSE_SHADER(STR) #STR
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 inline void SetColorAdjusterProperties( Actor& actor, const Vector3& hsvDelta, bool ignoreAlpha = false )
35 {
36   actor.RegisterProperty( "uHSVDelta", hsvDelta );
37   actor.RegisterProperty( "uIgnoreAlpha", ignoreAlpha ? 1.f : 0.f );
38 }
39
40 /**
41 * Creates a new ColorAdjuster effect.
42 * ColorAdjuster is a custom shader effect to adjust the image color in HSV space.
43 * @param[in] hsvDelta The color difference to apply to the HSV channel.
44 * @param[in] ignoreAlpha If true, the result color will be opaque even though source has alpha value
45 * @return A handle to a newly allocated Dali resource.
46 */
47 inline Property::Map CreateColorAdjuster()
48 {
49   std::string fragmentShader = DALI_COMPOSE_SHADER(
50     precision highp float;\n
51     uniform vec3 uHSVDelta;\n
52     uniform float uIgnoreAlpha;\n
53     varying mediump vec2 vTexCoord;\n
54     uniform sampler2D sTexture;\n
55     float rand(vec2 co) \n
56     {\n
57       return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n}
58     \n
59     vec3 rgb2hsv(vec3 c)\n
60     {\n
61       vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n
62       vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n
63       vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n
64       \n
65       float d = q.x - min(q.w, q.y);\n
66       float e = 1.0e-10;\n
67       return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n
68     }\n
69     vec3 hsv2rgb(vec3 c)\n
70     {\n
71       vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n
72       vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n
73       return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n
74     }\n
75     void main() {\n
76       vec4 color = texture2D(sTexture, vTexCoord); \n
77       vec3 hsvColor = rgb2hsv( color.rgb );\n
78       // modify the hsv Value
79       hsvColor += uHSVDelta * rand(vTexCoord); \n
80       // if the new vale exceeds one, then decrease it
81       hsvColor -= max(hsvColor*2.0 - vec3(2.0), 0.0);\n
82       // if the new vale drops below zero, then increase it
83       hsvColor -= min(hsvColor*2.0, 0.0);\n
84       color.rgb = hsv2rgb( hsvColor ); \n
85       // uIgnoreAlpha decide the result alpha will be 1.0 or source's alpha
86       color.a += uIgnoreAlpha;\n
87       gl_FragColor = color; \n
88     }\n
89   );
90
91   Property::Map customShader;
92   customShader[ "fragmentShader" ] = fragmentShader;
93
94   Property::Map map;
95   map[ "shader" ] = customShader;
96
97   return map;
98 }
99
100 } // namespace Internal
101
102 } // namespace Toolkit
103
104 } // namespace Dali
105
106 #endif /* __DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H_ */