|
| 1 | +--- |
| 2 | +title: Retrieving Themable Cell Color in RadSpreadProcessing |
| 3 | +description: Learn how to retrieve the actual cell color in RadSpreadProcessing when the color comes from the document theme. |
| 4 | +type: how-to |
| 5 | +page_title: How to Retrieve Cell Color from Theme in RadSpreadProcessing |
| 6 | +slug: retrieve-cell-color-radspreadprocessing |
| 7 | +tags: radspreadprocessing, document processing, cell color, themable color, pattern fill |
| 8 | +res_type: kb |
| 9 | +ticketid: 1656165 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +Let's import an Excel file with some cells formatted with color. Learn how to retrieve the cell color when the background color is set from MS Excel with a theme. |
| 21 | + |
| 22 | +It is possible to set a **Standard** Color (e.g. Yellow) or a **Theme** Color (e.g. Dark Teal, Accent 1) to a cell: |
| 23 | + |
| 24 | +|Standard Color|Theme Color| |
| 25 | +|----|----| |
| 26 | +| | | |
| 27 | + |
| 28 | +The *Yellow* color will be fixed and after the changing the document's theme, it wouldn't change. However, the *Dark Teal, Accent 1* color will be changed if another theme is selected: |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +This article demonstrates how to extract the color value from a cell when it is applied via a theme color. |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +To retrieve the cell color in RadSpreadProcessing, especially when the color is applied through the document theme, follow these steps: |
| 37 | + |
| 38 | +1. [Import the Excel document]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}) using the appropriate format provider. |
| 39 | +2. [Access the desired cell]({%slug radspreadprocessing-working-with-cells-accessing-cells-of-worksheet%}) or range of cells. |
| 40 | +3. Check if the cell's fill is of type [PatternFill](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties#fill-property). |
| 41 | +4. Retrieve the [ThemableColor]({%slug radspreadprocessing-features-styling-document-themes%}) object from the `PatternFill`. |
| 42 | +5. Use the [GetActualValue](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes#getting-actual-values) method of the `ThemableColor` object, passing in the document's theme, to get the actual color value. |
| 43 | + |
| 44 | +Here is a sample code snippet demonstrating these steps: |
| 45 | + |
| 46 | +```csharp |
| 47 | +string filePath = "Book1.xlsx"; |
| 48 | +Workbook workbook = new Workbook(); |
| 49 | +IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); |
| 50 | + |
| 51 | +using (Stream input = new FileStream(filePath, FileMode.Open)) |
| 52 | +{ |
| 53 | + workbook = formatProvider.Import(input); |
| 54 | +} |
| 55 | +Worksheet worksheet = workbook.Worksheets.First(); |
| 56 | +CellSelection selection = worksheet.Cells[0,1]; |
| 57 | +PatternFill solidPatternFill = selection.GetFill().Value as PatternFill; |
| 58 | +if (solidPatternFill != null) |
| 59 | +{ |
| 60 | + PatternType type = solidPatternFill.PatternType; |
| 61 | + ThemableColor patternColor = solidPatternFill.PatternColor; |
| 62 | + Color color = patternColor.LocalValue; |
| 63 | + ThemableColor bg = solidPatternFill.BackgroundColor; |
| 64 | + Color bgcolor = bg.LocalValue; |
| 65 | + |
| 66 | + Color actualColor = patternColor.GetActualValue(workbook.Theme); |
| 67 | + // The actual color is the same as Accent1 color of the colorScheme |
| 68 | + Debug.WriteLine("RGB: " + actualColor.R.ToString() + ", " + actualColor.G.ToString() + ", " + actualColor.B.ToString()); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +This approach ensures that even when a cell's color is derived from the document's theme, you can obtain the actual color value as displayed in the Excel file. |
| 73 | + |
| 74 | +## See Also |
| 75 | + |
| 76 | +- [Document Themes in RadSpreadProcessing]({%slug radspreadprocessing-features-styling-document-themes%}}) |
| 77 | +- [Getting Actual Values](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes#getting-actual-values) |
| 78 | + |
| 79 | +--- |
0 commit comments