2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "color-adjuster.h"
27 const std::string HSVDELTA_PROPERTY_NAME("uHSVDelta");
29 ColorAdjuster::ColorAdjuster()
33 //Call the Parent copy constructor to add reference to the implementation for this object
34 ColorAdjuster::ColorAdjuster( ShaderEffect handle )
35 : ShaderEffect( handle )
39 ColorAdjuster::~ColorAdjuster()
43 ColorAdjuster ColorAdjuster::New( const Vector3& hsvDelta, bool ignoreAlpha )
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"
51 " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
53 " vec3 rgb2hsv(vec3 c)\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"
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"
63 " vec3 hsv2rgb(vec3 c)\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"
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"
84 ShaderEffect effect = ShaderEffect::New("", fragmentShader);
85 ColorAdjuster handle( effect );
86 handle.SetUniform( "uHSVDelta", hsvDelta );
87 handle.SetUniform( "uIgnoreAlpha", ignoreAlpha?1.0f:0.0f );
91 std::string ColorAdjuster::GetHsvDeltaPropertyName() const
93 return HSVDELTA_PROPERTY_NAME;
96 } // namespace Toolkit