|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: Getting Started |
| 4 | +description: "Make your first steps with the Telerik UI for {{ site.framework }} ColorPicker component by following a complete step-by-step tutorial." |
| 5 | +slug: aspnetcore_colorpicker_getting_started |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the ColorPicker |
| 10 | + |
| 11 | +This tutorial explains how to set up a basic Telerik UI for {{ site.framework }} ColorPicker and highlights the major steps in the configuration of the component. |
| 12 | + |
| 13 | +You will initialize a ColorPicker component with predefined `gradient` and `palette` view types, and then change its appearance. Finally, you will learn how to handle the events of the ColorPicker in order to color an arbitrary container. |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | +@[template](/_contentTemplates/core/getting-started-prerequisites.md#repl-component-gs-prerequisites) |
| 18 | + |
| 19 | +## 1. Prepare the CSHTML File |
| 20 | + |
| 21 | +@[template](/_contentTemplates/core/getting-started-directives.md#gs-adding-directives) |
| 22 | + |
| 23 | +Optionally, you can structure the document by adding the desired HTML elements like headings, divs, and paragraphs. |
| 24 | + |
| 25 | +```HtmlHelper |
| 26 | + @using Kendo.Mvc.UI |
| 27 | + <h4>ColorPicker with a placeholder</h4> |
| 28 | + <div> |
| 29 | + |
| 30 | + </div> |
| 31 | +``` |
| 32 | +{% if site.core %} |
| 33 | +```TagHelper |
| 34 | + @addTagHelper *, Kendo.Mvc |
| 35 | + <h4>ColorPicker with a placeholder</h4> |
| 36 | + <div> |
| 37 | + |
| 38 | + </div> |
| 39 | +``` |
| 40 | +{% endif %} |
| 41 | + |
| 42 | + |
| 43 | +## 2. Initialize the ColorPicker |
| 44 | + |
| 45 | +Use the ColorPicker HtmlHelper {% if site.core %}or TagHelper{% endif %} to add the component to a page. |
| 46 | + |
| 47 | +The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the ColorPicker element. |
| 48 | + |
| 49 | +```HtmlHelper |
| 50 | + @(Html.Kendo().ColorPicker() |
| 51 | + .Name("colorPicker") |
| 52 | + ) |
| 53 | +``` |
| 54 | +{% if site.core %} |
| 55 | +```TagHelper |
| 56 | + <kendo-colorpicker name="colorPicker"> |
| 57 | + </kendo-colorpicker> |
| 58 | +``` |
| 59 | +{% endif %} |
| 60 | + |
| 61 | + |
| 62 | +## 3. Configure the Views |
| 63 | + |
| 64 | +The next step is to explicitly declare the Views configuration. The following example will configure the `gradient` and `palette` view types whilst providing a default view as the primary source of coloring. |
| 65 | + |
| 66 | + |
| 67 | +```HtmlHelper |
| 68 | + @(Html.Kendo().ColorPicker() |
| 69 | + .Name("colorPicker") |
| 70 | + .View(ColorPickerView.Palette) |
| 71 | + .Views(new string[] { "palette", "gradient"}) |
| 72 | + ) |
| 73 | +``` |
| 74 | + |
| 75 | +{% if site.core %} |
| 76 | +```TagHelper |
| 77 | + @{ |
| 78 | + string[] views = new string[] { "gradient", "palette" }; |
| 79 | + } |
| 80 | +
|
| 81 | + <kendo-colorpicker name="colorPicker" |
| 82 | + view="ColorPickerView.Palette" |
| 83 | + views="views"> |
| 84 | + </kendo-colorpicker> |
| 85 | +``` |
| 86 | +{% endif %} |
| 87 | + |
| 88 | +## 4. Specify the Formats |
| 89 | + |
| 90 | +The ColorPicker supports both RGB and HEX formats. The following example will configure the RGB and HEX formats, as well as a default format. |
| 91 | + |
| 92 | +```HtmlHelper |
| 93 | + @(Html.Kendo().ColorPicker() |
| 94 | + .Name("colorPicker") |
| 95 | + .View(ColorPickerView.Palette) |
| 96 | + .Views(new string[] { "palette", "gradient"}) |
| 97 | + .Format(ColorPickerFormat.Hex) |
| 98 | + .Formats(new string[] { "rgb", "hex" }) |
| 99 | + ) |
| 100 | +``` |
| 101 | + |
| 102 | +{% if site.core %} |
| 103 | +```TagHelper |
| 104 | + @{ |
| 105 | + string[] views = new string[] { "gradient", "palette" }; |
| 106 | + string[] formats = new string[] { "rgb", "hex" }; |
| 107 | + } |
| 108 | +
|
| 109 | + <kendo-colorpicker name="colorPicker" |
| 110 | + view="ColorPickerView.Palette" |
| 111 | + views="views" |
| 112 | + format="ColorPickerFormat.Hex" |
| 113 | + formats="formats"> |
| 114 | + </kendo-colorpicker> |
| 115 | +``` |
| 116 | +{% endif %} |
| 117 | + |
| 118 | + |
| 119 | +## 5. Customize the Appearance of ColorPicker |
| 120 | + |
| 121 | +To change the [appearance]({% slug appearance_colorpicker_aspnetcore %}) of the ColorPicker, use any of its built-in styling options, for example, `Size()`, `Rounded()`, and `FillMode()`. |
| 122 | + |
| 123 | +```HtmlHelper |
| 124 | + @(Html.Kendo().ColorPicker() |
| 125 | + .Name("colorPicker") |
| 126 | + .View(ColorPickerView.Palette) |
| 127 | + .Views(new string[] { "palette", "gradient"}) |
| 128 | + .Format(ColorPickerFormat.Hex) |
| 129 | + .Formats(new string[] { "rgb", "hex" }) |
| 130 | + .Size(ComponentSize.Medium) |
| 131 | + .Rounded(Rounded.Medium) |
| 132 | + .FillMode(FillMode.Solid) |
| 133 | + ) |
| 134 | +``` |
| 135 | + |
| 136 | +{% if site.core %} |
| 137 | +```TagHelper |
| 138 | + @{ |
| 139 | + string[] views = new string[] { "gradient", "palette" }; |
| 140 | + string[] formats = new string[] { "rgb", "hex" }; |
| 141 | + } |
| 142 | +
|
| 143 | + <kendo-colorpicker name="colorPicker" |
| 144 | + view="ColorPickerView.Palette" |
| 145 | + views="views" |
| 146 | + format="ColorPickerFormat.Hex" |
| 147 | + formats="formats" |
| 148 | + size="ComponentSize.Medium" |
| 149 | + rounded="Rounded.Medium" |
| 150 | + fillmode="FillMode.Solid"> |
| 151 | + </kendo-colorpicker> |
| 152 | +``` |
| 153 | +{% endif %} |
| 154 | + |
| 155 | +## 6. Handle the ColorPicker Events |
| 156 | + |
| 157 | +The ColorPicker component exposes various [events](https://docs.telerik.com/{{ site.platform }}/api/kendo.mvc.ui.fluent/colorpickerbuilder#eventssystemaction) that you can handle and further customize the functionality of the component or other proprietary HTML elements. In this tutorial, you will use the `Change` event to change the color of another container. |
| 158 | + |
| 159 | +```HtmlHelper |
| 160 | + @(Html.Kendo().ColorPicker() |
| 161 | + .Name("colorPicker") |
| 162 | + .View(ColorPickerView.Palette) |
| 163 | + .Views(new string[] { "palette", "gradient"}) |
| 164 | + .Format(ColorPickerFormat.Hex) |
| 165 | + .Formats(new string[] { "rgb", "hex" }) |
| 166 | + .Size(ComponentSize.Medium) |
| 167 | + .Rounded(Rounded.Medium) |
| 168 | + .FillMode(FillMode.Solid) |
| 169 | + .Events(events => events.Change("onChange")) |
| 170 | + ) |
| 171 | +
|
| 172 | + <script> |
| 173 | + function onChange(e) { |
| 174 | + $("#background").css("background-color", e.value); |
| 175 | + } |
| 176 | + </script> |
| 177 | +``` |
| 178 | +{% if site.core %} |
| 179 | +```TagHelper |
| 180 | + @{ |
| 181 | + string[] views = new string[] { "gradient", "palette" }; |
| 182 | + string[] formats = new string[] { "rgb", "hex" }; |
| 183 | + } |
| 184 | +
|
| 185 | + <kendo-colorpicker name="colorPicker" |
| 186 | + view="ColorPickerView.Palette" |
| 187 | + views="views" |
| 188 | + format="ColorPickerFormat.Hex" |
| 189 | + formats="formats" |
| 190 | + size="ComponentSize.Medium" |
| 191 | + rounded="Rounded.Medium" |
| 192 | + fillmode="FillMode.Solid" |
| 193 | + on-change="onChange"> |
| 194 | + </kendo-colorpicker> |
| 195 | +
|
| 196 | + <script> |
| 197 | + function onChange(e) { |
| 198 | + $("#background").css("background-color", e.value); |
| 199 | + } |
| 200 | + </script> |
| 201 | +``` |
| 202 | +{% endif %} |
| 203 | + |
| 204 | +## 7. (Optional) Reference Existing ColorPicker Instances |
| 205 | + |
| 206 | +You can reference the ColorPicker instances that you have created and build on top of their existing configuration: |
| 207 | + |
| 208 | +1. Use the `.Name()` (`id` attribute) of the component instance to get a reference. |
| 209 | + |
| 210 | + ```script |
| 211 | + <script> |
| 212 | + $(document).ready(function() { |
| 213 | + var colorPickerReference = $("#colorPicker").data("kendoColorPicker"); // colorPickerReference is a reference to the existing ColorPicker instance of the helper. |
| 214 | + }) |
| 215 | + </script> |
| 216 | + ``` |
| 217 | +1. Toggle the popup of the component by using the [`toggle()`](https://docs.telerik.com/kendo-ui/api/javascript/ui/colorpicker/methods/toggle) client-side method. |
| 218 | +
|
| 219 | + ```script |
| 220 | + <script> |
| 221 | + $(document).ready(function() { |
| 222 | + var colorPickerReference = $("#colorPicker").data("kendoColorPicker"); // colorPickerReference is a reference to the existing ColorPicker instance of the helper. |
| 223 | + colorPickerReference.toggle(); // Toggle the popup of the component. |
| 224 | + }) |
| 225 | + </script> |
| 226 | + ``` |
| 227 | +
|
| 228 | +
|
| 229 | +{% if site.core %} |
| 230 | +
|
| 231 | +## Explore this Tutorial in REPL |
| 232 | +
|
| 233 | +You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: |
| 234 | +
|
| 235 | +* [Sample code with the ColorPicker HtmlHelper](https://netcorerepl.telerik.com/cnEVwpYA19Js36bT53) |
| 236 | +* [Sample code with the ColorPicker TagHelper](https://netcorerepl.telerik.com/cdkhwJEq22Pa8xDN26) |
| 237 | +
|
| 238 | +{% endif %} |
| 239 | +
|
| 240 | +
|
| 241 | +## Next Steps |
| 242 | +
|
| 243 | +{% if site.core %} |
| 244 | +* [Configuring the Contrast Tool]({% slug htmlhelpers_contrast_tool_colorpickerhelper_aspnetcore %}) |
| 245 | +{% endif %} |
| 246 | +* [Customizing the Appearance of the ColorPicker]({% slug appearance_colorpicker_aspnetcore %}) |
| 247 | +
|
| 248 | +## See Also |
| 249 | +
|
| 250 | +* [Using the API of the ColorPicker for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/colorpicker/api) |
| 251 | +* [Client-Side API of the ColorPicker](https://docs.telerik.com/kendo-ui/api/javascript/ui/colorpicker) |
| 252 | +* [Server-Side API of the ColorPicker for {{ site.framework }}](/api/colorpicker) |
| 253 | +* [Knowledge Base Section](/knowledge-base) |
0 commit comments