2 * Copyright (c) 2021 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.
21 using System.Collections.Generic;
22 using System.Diagnostics;
23 using Tizen.NUI.BaseComponents;
24 using Tizen.NUI.Components;
26 namespace Tizen.NUI.Samples
28 class PaletteSample : IExample
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"
41 private int viewIndex = 0;
42 private int windowWidth, windowHeight;
43 private Window currentWindow;
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();
57 public void Activate()
62 public void Initialize()
64 currentWindow = NUIApplication.GetDefaultWindow();
65 currentWindow.BackgroundColor = Color.White;
67 windowWidth = Window.Instance.Size.Width;
68 windowHeight = Window.Instance.Size.Height;
70 CreatePage(viewIndex);
74 public void CreateBottomLayout()
76 bottomView = new View()
78 Size = new Size(windowWidth, bottomHeight),
79 Position2D = new Position2D(0, windowHeight - bottomHeight),
80 Layout = new LinearLayout()
82 LinearOrientation = LinearLayout.Orientation.Horizontal,
83 LinearAlignment = LinearLayout.Alignment.Center,
87 currentWindow.Add(bottomView);
89 Button prevBtn = new Button()
92 Size = new Size(buttonWeight, buttonHeight),
95 Button nextBtn = new Button()
98 Size = new Size(buttonWeight, buttonHeight),
101 bottomView.Add(prevBtn);
102 bottomView.Add(nextBtn);
104 prevBtn.Clicked += PrevClicked;
105 nextBtn.Clicked += NextClicked;
108 private void PrevClicked(object sender, ClickedEventArgs e)
110 if (viewIndex == 0) return;
114 CreatePage(viewIndex);
118 private void NextClicked(object sender, ClickedEventArgs e)
120 if (viewIndex == maxView) return;
124 CreatePage(viewIndex);
127 public void CreatePage(int idx)
131 Size = new Size(windowWidth, windowHeight - bottomHeight),
132 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical },
134 currentWindow.Add(view);
136 imageView = CreateImageView(viewIndex);
140 palette = ImageGenerate(viewIndex);
143 TextLabel label = new TextLabel("Time = " + timer.ElapsedMilliseconds.ToString() + "ms")
145 Size2D = new Size2D((int)(windowWidth), (int)((windowHeight - windowWidth) / 9)),
146 HorizontalAlignment = HorizontalAlignment.End,
147 VerticalAlignment = VerticalAlignment.Center,
151 dominantSwatch = palette.GetDominantSwatch();
152 if (dominantSwatch != null) {
153 CreateLabel(dominantSwatch);
156 vibrantSwatch = palette.GetVibrantSwatch();
157 if (vibrantSwatch != null) {
158 CreateLabel(vibrantSwatch);
161 mutedSwatch = palette.GetMutedSwatch();
162 if (mutedSwatch != null) {
163 CreateLabel(mutedSwatch);
166 darkVibrantSwatch = palette.GetDarkVibrantSwatch();
167 if (darkVibrantSwatch != null) {
168 CreateLabel(darkVibrantSwatch);
171 darkMutedSwatch = palette.GetDarkMutedSwatch();
172 if (darkMutedSwatch != null) {
173 CreateLabel(darkMutedSwatch);
176 lightVibrantSwatch = palette.GetLightVibrantSwatch();
177 if (lightVibrantSwatch != null) {
178 CreateLabel(lightVibrantSwatch);
181 lightMutedSwatch = palette.GetLightMutedSwatch();
182 if (lightMutedSwatch != null) {
183 CreateLabel(lightMutedSwatch);
189 public void CreateLabel(Palette.Swatch swatch)
191 Tizen.NUI.Color color = swatch.GetRgb();
193 string txt = " RGB(" + (int)(color.R * 255) + " " + (int)(color.G * 255) + " " + (int)(color.B * 255) + ")";
194 TextLabel label = new TextLabel(txt)
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,
206 public Palette ImageGenerate(int idx)
208 Bitmap bitmap = new Bitmap(imagePath[idx]);
209 Palette palette = Palette.generate(bitmap);
214 public ImageView CreateImageView(int idx)
216 ImageView tempImage = new ImageView()
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,
227 public void Deactivate()
229 //Will Do FullGC in DailDemo Class
231 bottomView.Unparent();
234 bottomView.Dispose();