Fix prevent issues
[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
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace
32 {
33 const unsigned int MAX_BORDER_SIZE( 9 );
34 }
35
36 ImageActor CreateSolidColorActor( const Vector4& color, bool border, const Vector4& borderColor, const unsigned int borderSize )
37 {
38   ImageActor image;
39   if( borderSize > MAX_BORDER_SIZE )
40   {
41     return image;
42   }
43
44   const unsigned int bitmapWidth = borderSize * 2 + 2;
45   BufferImage imageData = BufferImage::New( bitmapWidth, bitmapWidth, Pixel::RGBA8888 );
46
47   // Create the image
48   PixelBuffer* pixbuf = imageData.GetBuffer();
49   if( !pixbuf )
50   {
51     return image;
52   }
53
54   Vector4 outerColor = color;
55   if ( border )
56   {
57     outerColor = borderColor;
58   }
59
60   // Using a (2 + border) x (2 + border) image gives a better blend with the GL implementation
61   // than a (1 + border) x (1 + border) image
62   const unsigned int bitmapSize = bitmapWidth * bitmapWidth;
63   const unsigned int topLeft = bitmapWidth * borderSize + borderSize;
64   const unsigned int topRight = topLeft + 1;
65   const unsigned int bottomLeft = bitmapWidth * (borderSize + 1) + borderSize;
66   const unsigned int bottomRight = bottomLeft + 1;
67
68   for( size_t i = 0; i < bitmapSize; ++i )
69   {
70     if( i == topLeft ||
71         i == topRight ||
72         i == bottomLeft ||
73         i == bottomRight )
74     {
75       pixbuf[i*4+0] = 0xFF * color.r;
76       pixbuf[i*4+1] = 0xFF * color.g;
77       pixbuf[i*4+2] = 0xFF * color.b;
78       pixbuf[i*4+3] = 0xFF * color.a;
79     }
80     else
81     {
82       pixbuf[i*4+0] = 0xFF * outerColor.r;
83       pixbuf[i*4+1] = 0xFF * outerColor.g;
84       pixbuf[i*4+2] = 0xFF * outerColor.b;
85       pixbuf[i*4+3] = 0xFF * outerColor.a;
86     }
87   }
88
89   imageData.Update();
90   image = ImageActor::New( imageData );
91   image.SetParentOrigin( ParentOrigin::CENTER );
92
93   if( border )
94   {
95     image.SetStyle( ImageActor::STYLE_NINE_PATCH );
96     image.SetNinePatchBorder( Vector4::ONE * (float)borderSize * 2.0f );
97   }
98
99   return image;
100 }
101
102 } // namespace Toolkit
103
104 } // namespace Dali