[dali_1.1.29] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / default-controls / solid-color-actor.cpp
1 /*
2  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
19 #include <dali/public-api/images/buffer-image.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace
31 {
32 const unsigned int MAX_BORDER_SIZE( 9 );
33 }
34
35 ImageActor CreateSolidColorActor( const Vector4& color, bool border, const Vector4& borderColor, const unsigned int borderSize )
36 {
37   ImageActor image;
38   if( borderSize > MAX_BORDER_SIZE )
39   {
40     return image;
41   }
42
43   const unsigned int bitmapWidth = borderSize * 2 + 2;
44   bool needAlphaChannel = (color.a < 1.0f) || ( border && borderColor.a < 1.0f );
45   BufferImage imageData;
46   if( needAlphaChannel )
47   {
48     imageData = BufferImage::New( bitmapWidth, bitmapWidth, Pixel::RGBA8888 );
49   }
50   else
51   {
52     imageData = BufferImage::New( bitmapWidth, bitmapWidth, Pixel::RGB888 );
53   }
54
55   // Create the image
56   PixelBuffer* pixbuf = imageData.GetBuffer();
57   if( !pixbuf )
58   {
59     return image;
60   }
61
62   Vector4 outerColor = color;
63   if ( border )
64   {
65     outerColor = borderColor;
66   }
67
68   // Using a (2 + border) x (2 + border) image gives a better blend with the GL implementation
69   // than a (1 + border) x (1 + border) image
70   const unsigned int bitmapSize = bitmapWidth * bitmapWidth;
71   const unsigned int topLeft = bitmapWidth * borderSize + borderSize;
72   const unsigned int topRight = topLeft + 1;
73   const unsigned int bottomLeft = bitmapWidth * (borderSize + 1) + borderSize;
74   const unsigned int bottomRight = bottomLeft + 1;
75
76   if( needAlphaChannel )
77   {
78     for( size_t i = 0; i < bitmapSize; ++i )
79     {
80       if( i == topLeft ||
81           i == topRight ||
82           i == bottomLeft ||
83           i == bottomRight )
84       {
85         pixbuf[i*4+0] = 0xFF * color.r;
86         pixbuf[i*4+1] = 0xFF * color.g;
87         pixbuf[i*4+2] = 0xFF * color.b;
88         pixbuf[i*4+3] = 0xFF * color.a;
89       }
90       else
91       {
92         pixbuf[i*4+0] = 0xFF * outerColor.r;
93         pixbuf[i*4+1] = 0xFF * outerColor.g;
94         pixbuf[i*4+2] = 0xFF * outerColor.b;
95         pixbuf[i*4+3] = 0xFF * outerColor.a;
96       }
97     }
98   }
99   else
100   {
101     for( size_t i = 0; i < bitmapSize; ++i )
102     {
103       if( i == topLeft ||
104           i == topRight ||
105           i == bottomLeft ||
106           i == bottomRight )
107       {
108         pixbuf[i*3+0] = 0xFF * color.r;
109         pixbuf[i*3+1] = 0xFF * color.g;
110         pixbuf[i*3+2] = 0xFF * color.b;
111       }
112       else
113       {
114         pixbuf[i*3+0] = 0xFF * outerColor.r;
115         pixbuf[i*3+1] = 0xFF * outerColor.g;
116         pixbuf[i*3+2] = 0xFF * outerColor.b;
117       }
118     }
119   }
120
121   imageData.Update();
122   image = ImageActor::New( imageData );
123   image.SetParentOrigin( ParentOrigin::CENTER );
124
125   if( border )
126   {
127     image.SetStyle( ImageActor::STYLE_NINE_PATCH );
128     image.SetNinePatchBorder( Vector4::ONE * (float)borderSize * 2.0f );
129   }
130
131   return image;
132 }
133
134 } // namespace Toolkit
135
136 } // namespace Dali