[NUI] Introduce NUI Palette APIs
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / PaletteSample.cs
1 /*
2 * Copyright (c) 2021 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 using System;
19 using Tizen.NUI;
20 using System.Collections.Generic;
21 using System.Diagnostics;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Components;
24
25 namespace Tizen.NUI.Samples
26 {
27     class PaletteSample : IExample
28     {
29         private static int bottomHeight = 60;
30         private static int buttonWeight = 100;
31         private static int buttonHeight = 40;
32         private static int maxView = 2;
33         private static string resourcePath = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
34         private static string[] imagePath = {
35             resourcePath + "/images/PaletteTest/rock.jpg",
36             resourcePath + "/images/PaletteTest/red2.jpg",
37             resourcePath + "/images/PaletteTest/10by10tree.png",
38             resourcePath + "/images/PaletteTest/3color.jpeg"
39         };
40
41         private int viewIndex = 0;
42         private int windowWidth, windowHeight;
43         private Window currentWindow;
44         private View view;
45         private View bottomView;
46         private ImageView imageView;
47         private Palette palette;
48         private Palette.Swatch dominantSwatch;
49         private Palette.Swatch vibrantSwatch;
50         private Palette.Swatch mutedSwatch;
51         private Palette.Swatch darkVibrantSwatch;
52         private Palette.Swatch darkMutedSwatch;
53         private Palette.Swatch lightVibrantSwatch;
54         private Palette.Swatch lightMutedSwatch;
55         private Stopwatch timer = new Stopwatch();
56
57         public void Activate()
58         {
59             Initialize();
60         }
61
62         public void Initialize()
63         {
64             currentWindow = NUIApplication.GetDefaultWindow();
65             currentWindow.BackgroundColor = Color.White;
66
67             windowWidth = Window.Instance.Size.Width;
68             windowHeight = Window.Instance.Size.Height;
69
70             CreatePage(viewIndex);
71             CreateBottomLayout();
72
73         }
74
75         public void CreateBottomLayout()
76         {
77             bottomView = new View()
78             {
79                 Size = new Size(windowWidth, bottomHeight),
80                 Position2D = new Position2D(0, windowHeight - bottomHeight),
81                 Layout = new LinearLayout()
82                 {
83                     LinearOrientation = LinearLayout.Orientation.Horizontal,
84                     LinearAlignment = LinearLayout.Alignment.Center,
85                 }
86
87             };
88             currentWindow.Add(bottomView);
89
90             Button prevBtn = new Button()
91             {
92                 Text = "Prev",
93                 Size = new Size(buttonWeight, buttonHeight),
94                 Margin = 10,
95             };
96             Button nextBtn = new Button()
97             {
98                 Text = "next",
99                 Size = new Size(buttonWeight, buttonHeight),
100                 Margin = 10,
101             };
102             bottomView.Add(prevBtn);
103             bottomView.Add(nextBtn);
104
105             prevBtn.Clicked += PrevClicked;
106             nextBtn.Clicked += NextClicked;
107         }
108
109         private void PrevClicked(object sender, ClickedEventArgs e)
110         {
111             if (viewIndex == 0) return;
112
113             viewIndex--;
114             view.Unparent();
115             CreatePage(viewIndex);
116
117         }
118
119         private void NextClicked(object sender, ClickedEventArgs e)
120         {
121             if (viewIndex == maxView) return;
122
123             viewIndex++;
124             view.Unparent();
125             CreatePage(viewIndex);
126         }
127
128         public void CreatePage(int idx)
129         {
130             view = new View()
131             {
132                 Size = new Size(windowWidth, windowHeight - bottomHeight),
133                 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical },
134             };
135             currentWindow.Add(view);
136
137             imageView = CreateImageView(viewIndex);
138             view.Add(imageView);
139
140             timer.Start();
141             palette = ImageGenerate(viewIndex);
142             timer.Stop();
143
144             TextLabel label = new TextLabel("Time = " + timer.ElapsedMilliseconds.ToString() + "ms")
145             {
146                 Size2D  = new Size2D((int)(windowWidth), (int)((windowHeight - windowWidth) / 9)),
147                 HorizontalAlignment = HorizontalAlignment.End,
148                 VerticalAlignment = VerticalAlignment.Center,
149             };
150             view.Add(label);
151
152             dominantSwatch = palette.GetDominantSwatch();
153              if (dominantSwatch != null) {
154                 CreateLabel(dominantSwatch);
155             }
156
157             lightVibrantSwatch = palette.GetLightVibrantSwatch();
158             if (lightVibrantSwatch != null) {
159                 CreateLabel(lightVibrantSwatch);
160             }
161
162             vibrantSwatch = palette.GetVibrantSwatch();
163             if (vibrantSwatch != null) {
164                 CreateLabel(vibrantSwatch);
165             }
166
167             darkVibrantSwatch = palette.GetDarkVibrantSwatch();
168             if (darkVibrantSwatch != null) {
169                 CreateLabel(darkVibrantSwatch);
170             }
171
172             lightMutedSwatch = palette.GetLightMutedSwatch();
173             if (lightMutedSwatch != null) {
174                 CreateLabel(lightMutedSwatch);
175             }
176
177             mutedSwatch = palette.GetMutedSwatch();
178             if (mutedSwatch != null) {
179                 CreateLabel(mutedSwatch);
180             }
181
182             darkMutedSwatch = palette.GetDarkMutedSwatch();
183             if (darkMutedSwatch != null) {
184                 CreateLabel(darkMutedSwatch);
185             }
186
187             timer.Reset();
188         }
189
190         public void CreateLabel(Palette.Swatch swatch)
191         {
192             Color color = swatch.GetRgb();
193
194             string txt = " RGB(" + (int)(color.R * 255) + " " + (int)(color.G * 255) + " " + (int)(color.B * 255) + ")";
195             TextLabel label = new TextLabel(txt)
196             {
197                 TextColor = swatch.GetBodyTextColor(),
198                 BackgroundColor = color,
199                 Size2D  = new Size2D((int)(windowWidth), (int)((windowHeight - windowWidth) / 9)),
200                 HorizontalAlignment = HorizontalAlignment.Begin,
201                 VerticalAlignment = VerticalAlignment.Center,
202             };
203
204             view.Add(label);
205         }
206
207         public Palette ImageGenerate(int idx)
208         {
209             PixelBuffer imgBitmap = ImageLoading.LoadImageFromFile(imagePath[idx]);
210             Palette palette = Palette.generate(imgBitmap);
211             
212             return palette;
213         }
214
215         public ImageView CreateImageView(int idx)
216         {
217             ImageView tempImage = new ImageView()
218             {
219                 ResourceUrl = imagePath[idx],
220                 Size = new Tizen.NUI.Size(Window.Instance.Size.Width, Window.Instance.Size.Width),
221                 HeightResizePolicy = ResizePolicyType.Fixed,
222                 WidthResizePolicy = ResizePolicyType.Fixed,
223             };
224
225             return tempImage;
226         }
227
228         public void Deactivate()
229         {
230             //Will Do FullGC in DailDemo Class
231             view.Unparent();
232             bottomView.Unparent();
233
234             view.Dispose();
235             bottomView.Dispose();
236
237             view = null;
238             bottomView = null;
239         }
240     }
241 }