1 #ifndef DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H
2 #define DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/object/property-map.h>
26 #include <dali-toolkit/public-api/visuals/visual-properties.h>
28 #define DALI_COMPOSE_SHADER(STR) #STR
39 inline void SetColorAdjusterProperties( Actor& actor, const Vector3& hsvDelta, bool ignoreAlpha = false )
41 actor.RegisterProperty( "uHSVDelta", hsvDelta );
42 actor.RegisterProperty( "uIgnoreAlpha", ignoreAlpha ? 1.f : 0.f );
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.
52 inline Property::Map CreateColorAdjuster()
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
62 return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n}
64 vec3 rgb2hsv(vec3 c)\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
70 float d = q.x - min(q.w, q.y);\n
72 return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n
74 vec3 hsv2rgb(vec3 c)\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
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
96 Property::Map customShader;
97 customShader[ Toolkit::Visual::Shader::Property::FRAGMENT_SHADER ] = fragmentShader;
100 map[ Toolkit::Visual::Property::SHADER ] = customShader;
105 } // namespace Internal
107 } // namespace Toolkit
111 #endif // DALI_TOOLKIT_INTERNAL_COLOR_ADJUSTER_H