# Getting Started with the ColorPalette

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC ColorPalette and highlights the major steps in the configuration of the component.

You will initialize a ColorPalette control with a custom set of colors. Next, you will handle the  [`Change` event](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/simplecolorpickereventbuilder#changesystemfunc) of the component to get the selected color and apply it as a background color to [DropDownButtons](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/dropdownbutton/overview).

![Sample Telerik UI for ASP.NET MVC ColorPalette](https://www.telerik.com/aspnet-mvc/documentation/assets/2047c66fdfc295ff6578a9b93a72f34d/color-palette-getting-started.png)

## Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:

* To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can [use a project template](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-nuget).

## 1. Prepare the CSHTML File

The first step is to add the required directives at the top of the `.cshtml` document:

* To use the Telerik UI for ASP.NET MVC HtmlHelpers:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

Optionally, you can structure the View content by adding the desired HTML elements like headings, divs, paragraphs, and others.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    <div class="main-container">
        <div class="colorpalette-container">
            <h4>Select a color:</h4>
        </div>
    </div>
```

## 2. Initialize the ColorPalette

Use the ColorPalette HtmlHelper  to add the component to a page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the ColorPalette element.
* The `Palette()` option sets the desired range of colors that the user can pick from.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    <div class="main-container">
        <div class="colorpalette-container">
            <h4>Select a color:</h4>
            @(Html.Kendo().ColorPalette()
                .Name("colorpalette")
                .Palette(new string[] {
                    "#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
                })
            )
        </div>
    </div>
```

## 3. Implement the Color Changing

The next step is to configure DropDownButtons and handle the ColorPalette color selection. The `Change` event handler allows you to access the selected color. Iterate through the DropDownButtons on the page and change the background color of each button by using the jQuery [`css()`](http://api.jquery.com/css/) method.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    <div class="main-container">
        <div class="colorpalette-container">
            <h4>Select a color:</h4>
            @(Html.Kendo().ColorPalette()
            .Name("colorpalette")
            .Palette(new string[] {
                "#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
            })
            .Events(ev=>ev.Change("onChange")))
        </div>
        <div class="buttons-container">
            <div class='column'>
                <h4>Default Button:</h4>
                @(Html.Kendo().DropDownButton()
                    .Name("default")
                    .Text("Paste")
                    .Icon("clipboard")
                    .Items(items=>{
                        items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
                        items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
                        items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
                        items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
                    })
                )
            </div>
            <div class='column'>
                <h4>Outline Button:</h4>
                @(Html.Kendo().DropDownButton()
                    .Name("outline")
                    .Text("Paste")
                    .FillMode(FillMode.Outline)
                    .Icon("clipboard")
                    .Items(items=>{
                        items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
                        items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
                        items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
                        items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
                    })
                )
            </div>
            <div class='column'>
                <h4>Primary Button:</h4>
                @(Html.Kendo().DropDownButton()
                    .Name("primary")
                    .Text("Paste")
                    .ThemeColor(ThemeColor.Primary)
                    .Icon("clipboard")
                    .Items(items=>{
                        items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
                        items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
                        items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
                        items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
                    })
                )
            </div>
        </div>
    </div>
```

```JS scripts
    <script>
        function onChange(e){
            let selectedColor = e.value;
            $.each($("[data-role='dropdownbutton']"), function(){
                $(this).css("background-color", selectedColor)
            });
        }
    </script>
```

## 4. Customize the Appearance of ColorPalette

You can customize the overall ColorPalette appearance by using the following options:

* Use the [`Columns()`](https://docs.telerik.com/aspnet-core/api/kendo.mvc.ui.fluent/colorpalettebuilder#columnssystemint32) method to specify the required number of columns.
* The [`TileSize()`](https://docs.telerik.com/aspnet-core/api/kendo.mvc.ui.fluent/colorpalettebuilder#tilesizesystemaction) configuration allows you to define the size of the color cell.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    <div class="main-container">
        <div class="colorpalette-container">
            <h4>Select a color:</h4>
            @(Html.Kendo().ColorPalette()
                .Name("colorpalette")
                .Palette(new string[] {
                    "#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
                })
                .Columns(4)
                .TileSize(ts => ts.Width(50).Height(50))
            )
        </div>
    </div>
```

## 5. (Optional) Reference Existing ColorPalette Instances

You can reference the ColorPalette instances that you have created and build on top of their existing configuration:

1. Use the value of the `Name()` option of the component to establish a reference.

   ```JS script
        <script>
            $(document).ready(function() {
                var colorPaletteReference = $("#colorpalette").data("kendoColorPalette"); // colorPaletteReference is a reference to the existing ColorPalette instance of the helper.
            });
        </script>
   ```

2. Use the [ColorPalette client-side API](https://docs.telerik.com/kendo-ui/api/ui/colorpalette#methods) to control the behavior of the component. In this example, you will use the `enable` method to disable the control dynamically (for example, when a button is clicked).

   ```Razor HtmlHelper
       @(Html.Kendo().Button()
           .Name("btn")
           .Content("Disable ColorPalette")
           .Events(ev => ev.Click("onBtnClick")))
   ```

   ```JS scripts
       <script>
           function onBtnClick() {
               var colorPaletteReference = $("#colorpalette").data("kendoColorPalette");
               colorPaletteReference.enable(false);
           }
       </script>
   ```

## Next Steps

* [Configuring Color Presets of the ColorPalette](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/colorpalette/presets)
* [Using the Keyboard Navigation of the ColorPalette for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-core/colorpalette/keyboard-navigation)

## See Also

* [Using the API of the ColorPalette for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/colorpalette/api)
* [Client-Side API of the ColorPalette](https://docs.telerik.com/kendo-ui/api/ui/colorpalette)
* [Server-Side API of the ColorPalette](https://www.telerik.com/aspnet-mvc/documentation/api/colorpalette)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
