# Sample: Color palettes sample

## Overview

Use this sample to configure the values of the color pickers in the widget designers. With this sample, you create a widget named *Colors* that displays two color selectors – one with default colors, and the other with a custom-defined colors.

<SfDetails summary="Use the default palette">

The name of the palette that is used for the widget selectors by default is called `Default`. This palette is used for the built-in widgets, such as the *Section* widget. Additionally, you can reuse it for other types of widgets by using the following attribute decoration:

```C#
public class UseDefaultPalette
{
    //...
    [ColorPalette("Default")]
    public string Color { get; set; }

    //...
}
```

</SfDetails>

<SfDetails summary="Customize the default color palette">

You can override it in the `appsettings.json` file, like in the following example:

```JSON
"Widgets": {
    "Styling": {
      "ColorPalettes": {
        "Default": {
          "DefaultColor": "#AABBCC",
          "Colors": ["#AABBCC", "#BBAACC", "#CCBBAA"]
        },
      }
    }
  }
```

</SfDetails>

> info **PREREQUISITES**: You must set up a Sitefinity renderer application and connect it to your Sitefinity CMS application. For more information, see [Install Sitefinity in ASP.NET Core mode](https://www.progress.com/documentation/sitefinity-cms/sitefinity-installation).

> info **NOTE**: The instructions in this sample use Visual Studio 2022 and a Sitefinity renderer project named `Renderer`.

## Create the folder structure

Under your Renderer project, you must create the following folders:

* `Entities`/`Colors`
* `ViewComponents`
* `Views`/`Shared`/`Components`/`Colors`

## Create the widget

In Visual Studio, open the `Renderer` application.

<SfDetails>

<SfDetailsSummary>

1. Create the `Entity`

</SfDetailsSummary>

1. In the context menu of folder `Entities/Colors`, click *Add* » *Class…*
2. In *Name*, enter `ColorsEntity.cs` and click *Add*.
3. In the class, paste the following code and save your changes:

```C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Progress.Sitefinity.Renderer.Entities.Content;
using Progress.Sitefinity.AspNetCore.ViewComponents.AttributeConfigurator.Attributes;
using Progress.Sitefinity.Renderer.Designers;
using Progress.Sitefinity.Renderer.Designers.Attributes;
using Progress.Sitefinity.Renderer.Models;
using Progress.Sitefinity.Renderer.Entities;
using Progress.Sitefinity.AspNetCore;
using Progress.Sitefinity.AspNetCore.Models;
using Progress.Sitefinity.AspNetCore.ViewComponents;

namespace Renderer.Entities.Colors
{
    public class ColorsEntity
    {

        [ColorPalette("Default")]
        public string DefaultColor { get; set; }

        [ColorPalette("Custom")]
        public string CustomColor { get; set; }
    }
}
```

</SfDetails>

<SfDetails>

<SfDetailsSummary>

2. Create the `ViewComponent.cs` file

</SfDetailsSummary>

1. In the context menu of folder `ViewComponents`, click *Add* » *Class…*
2. In *Name*, enter `ColorViewComponent.cs` and click *Add*.
3. In the class, paste the following code and save your changes:

```C#
using System;
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.AspNetCore.ViewComponents;
using Newtonsoft.Json.Linq;
using Renderer.Entities.Colors;

namespace Renderer.ViewComponents.Colors
{
    /// <summary>
    /// Test widget with all property variations.
    /// </summary>
    [SitefinityWidget]
    public class ColorsViewComponent : ViewComponent
    {
        /// <summary>
        /// Invokes the view.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IViewComponentResult Invoke(IViewComponentContext<ColorsEntity> context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return this.View("Default", context.Entity);
        }
    }
}
```

</SfDetails>

<SfDetails>

<SfDetailsSummary>

3. Create the `View`

</SfDetailsSummary>

1. In the context menu of folder `Views/Shared/Components/Color`, click *Add* » *Class…*
2. Select *Code File*.
3. In *Name*, enter `Default.cshtml` and click *Add*.
4. In the class, paste the following code and save your changes:
   ```Razor
   @using Progress.Sitefinity.AspNetCore.Mvc.Rendering;

   @model Renderer.Entities.Colors.ColorsEntity

   <h1 style="background-color: @Model.DefaultColor;">
      Default color palette color 
   </h1>
   <h1 style="background-color: @Model.CustomColor;">
      Custom color palette color 
   </h1>
   ```

</SfDetails>

## Declare the custom color pallette

You need to declare the custom color palette in the application settings. You need to add the declaration in section `"Widgets"`/`"Styling"`/`"Colorpalettes`".

<SfDetails>

<SfDetailsSummary>

Modify the `appsettings.json` file

</SfDetailsSummary>

The `appsettings.json` file of your `Renderer` application should look similar to the following:

```JSON
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Sitefinity": {
    "Url": "https://yoursitefinitywebsiteurl",
    "WebServicePath": "api/default"
  },
  "Widgets": {
    "Styling": {
      "ColorPalettes": {
        "Default": {
          "DefaultColor": "#AABBCC",
          "Colors": ["#AABBCC", "#BBAACC", "#CCBBAA"]
        },

        "Custom": {
          "DefaultColor": "#DDEEFF",
          "Colors": ["#DDEEFF", "#EEDDFF", "#FFEEDD"]
        }
      }
    }
  }
}
```

</SfDetails>

Build your solution.

## Result

When you open your `Renderer` application and open the New editor, you will see the *Colors* widget in the widget selector. When you add the widget on your page, it displays two lines of text, colored differently. By editing the widget, you can change the colors - for the first line, you pick from the default color picker, for the second line - from the custom color picker.

![ColorPalettes](https://www.progress.com/documentation/sitefinity-cms/assets/f9e32423bf7a907b01f1aecb6d69de0f/colorpalettes.jpg "ColorPalettes")

## Run the sample

This sample is available in Sitefinity’s GitHub repository. You can run and play with it.\
To do this, perform the following:

1. Go to Sitefinity’s GitHub repository [Sitefinity ASP.NET Core samples](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples).
2. Expand *Code* and click *Download ZIP*.
3. Extract the files on your computer.
4. In the extracted folder, navigate to `sitefinity-aspnetcore-mvc-samples-master/src/color-palettes` folder.
5. Open the `color-palettes.sln` in Visual Studio.
6. Open the `appsettings.json` file.
7. In section `"Sitefinity"`, change the `"Url"` property to the URL of your Sitefinity CMS site.\
   If you have deployed Sitefinity CMS on the IIS, point to `https://localhost:<https_port>`.
8. In Visual Studio, in the context menu of `color-palettes` project, click *View in Browser*.
9. Log in to your Sitefinity CMS instance and place the widget on a page.
