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