18100313cefd9831bece85ec66b5c0c9b7e60b17
[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) 2016 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
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/object/property-map.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/visuals/visual-properties.h>
27
28 #define DALI_COMPOSE_SHADER(STR) #STR
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 inline void SetColorAdjusterProperties( Actor& actor, const Vector3& hsvDelta, bool ignoreAlpha = false )
40 {
41   actor.RegisterProperty( "uHSVDelta", hsvDelta );
42   actor.RegisterProperty( "uIgnoreAlpha", ignoreAlpha ? 1.f : 0.f );
43 }
44
45 /**
46 * Creates a new ColorAdjuster effect.
47 * ColorAdjuster is a custom shader effect to adjust the image color in HSV space.
48 * @param[in] hsvDelta The color difference to apply to the HSV channel.
49 * @param[in] ignoreAlpha If true, the result color will be opaque even though source has alpha value
50 * @return A handle to a newly allocated Dali resource.
51 */
52 inline Property::Map CreateColorAdjuster()
53 {
54   std::string fragmentShader = DALI_COMPOSE_SHADER(
55     precision highp float;\n
56     uniform vec3 uHSVDelta;\n
57     uniform float uIgnoreAlpha;\n
58     varying mediump vec2 vTexCoord;\n
59     uniform sampler2D sTexture;\n
60     float rand(vec2 co) \n
61     {\n
62       return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n}
63     \n
64     vec3 rgb2hsv(vec3 c)\n
65     {\n
66       vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n
67       vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n
68       vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n
69       \n
70       float d = q.x - min(q.w, q.y);\n
71       float e = 1.0e-10;\n
72       return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n
73     }\n
74     vec3 hsv2rgb(vec3 c)\n
75     {\n
76       vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n
77       vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n
78       return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n
79     }\n
80     void main() {\n
81       vec4 color = texture2D(sTexture, vTexCoord); \n
82       vec3 hsvColor = rgb2hsv( color.rgb );\n
83       // modify the hsv Value
84       hsvColor += uHSVDelta * rand(vTexCoord); \n
85       // if the new vale exceeds one, then decrease it
86       hsvColor -= max(hsvColor*2.0 - vec3(2.0), 0.0);\n
87       // if the new vale drops below zero, then increase it
88       hsvColor -= min(hsvColor*2.0, 0.0);\n
89       color.rgb = hsv2rgb( hsvColor ); \n
90       // uIgnoreAlpha decide the result alpha will be 1.0 or source's alpha
91       color.a += uIgnoreAlpha;\n
92       gl_FragColor = color; \n
93     }\n
94   );
95
96   Property::Map customShader;
97   customShader[ Toolkit::Visual::Shader::Property::FRAGMENT_SHADER ] = fragmentShader;
98
99   Property::Map map;
100   map[ Toolkit::Visual::Property::SHADER ] = customShader;
101
102   return map;
103 }
104
105 } // namespace Internal
106
107 } // namespace Toolkit
108
109 } // namespace Dali
110
111 #endif // DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H