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.
20 using System.Collections.Generic;
21 using System.Diagnostics;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Components;
25 namespace Tizen.NUI.Samples
27 class PaletteSample : IExample
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"
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);
75 public void CreateBottomLayout()
77 bottomView = new View()
79 Size = new Size(windowWidth, bottomHeight),
80 Position2D = new Position2D(0, windowHeight - bottomHeight),
81 Layout = new LinearLayout()
83 LinearOrientation = LinearLayout.Orientation.Horizontal,
84 LinearAlignment = LinearLayout.Alignment.Center,
88 currentWindow.Add(bottomView);
90 Button prevBtn = new Button()
93 Size = new Size(buttonWeight, buttonHeight),
96 Button nextBtn = new Button()
99 Size = new Size(buttonWeight, buttonHeight),
102 bottomView.Add(prevBtn);
103 bottomView.Add(nextBtn);
105 prevBtn.Clicked += PrevClicked;
106 nextBtn.Clicked += NextClicked;
109 private void PrevClicked(object sender, ClickedEventArgs e)
111 if (viewIndex == 0) return;
115 CreatePage(viewIndex);
119 private void NextClicked(object sender, ClickedEventArgs e)
121 if (viewIndex == maxView) return;
125 CreatePage(viewIndex);
128 public void CreatePage(int idx)
132 Size = new Size(windowWidth, windowHeight - bottomHeight),
133 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical },
135 currentWindow.Add(view);
137 imageView = CreateImageView(viewIndex);
141 palette = ImageGenerate(viewIndex);
144 TextLabel label = new TextLabel("Time = " + timer.ElapsedMilliseconds.ToString() + "ms")
146 Size2D = new Size2D((int)(windowWidth), (int)((windowHeight - windowWidth) / 9)),
147 HorizontalAlignment = HorizontalAlignment.End,
148 VerticalAlignment = VerticalAlignment.Center,
152 dominantSwatch = palette.GetDominantSwatch();
153 if (dominantSwatch != null) {
154 CreateLabel(dominantSwatch);
157 lightVibrantSwatch = palette.GetLightVibrantSwatch();
158 if (lightVibrantSwatch != null) {
159 CreateLabel(lightVibrantSwatch);
162 vibrantSwatch = palette.GetVibrantSwatch();
163 if (vibrantSwatch != null) {
164 CreateLabel(vibrantSwatch);
167 darkVibrantSwatch = palette.GetDarkVibrantSwatch();
168 if (darkVibrantSwatch != null) {
169 CreateLabel(darkVibrantSwatch);
172 lightMutedSwatch = palette.GetLightMutedSwatch();
173 if (lightMutedSwatch != null) {
174 CreateLabel(lightMutedSwatch);
177 mutedSwatch = palette.GetMutedSwatch();
178 if (mutedSwatch != null) {
179 CreateLabel(mutedSwatch);
182 darkMutedSwatch = palette.GetDarkMutedSwatch();
183 if (darkMutedSwatch != null) {
184 CreateLabel(darkMutedSwatch);
190 public void CreateLabel(Palette.Swatch swatch)
192 Color color = swatch.GetRgb();
194 string txt = " RGB(" + (int)(color.R * 255) + " " + (int)(color.G * 255) + " " + (int)(color.B * 255) + ")";
195 TextLabel label = new TextLabel(txt)
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,
207 public Palette ImageGenerate(int idx)
209 PixelBuffer imgBitmap = ImageLoading.LoadImageFromFile(imagePath[idx]);
210 Palette palette = Palette.generate(imgBitmap);
215 public ImageView CreateImageView(int idx)
217 ImageView tempImage = new ImageView()
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,
228 public void Deactivate()
230 //Will Do FullGC in DailDemo Class
232 bottomView.Unparent();
235 bottomView.Dispose();