如何使用颜色调色板
颜色和调色板
调色板是在创建图像时可用的颜色数量。在演示文稿中使用标准调色板可以让用户创建一致的外观。每个Microsoft Excel(97-2003)文件都有一个包含可应用于单元格、字体、网格线、图形对象、填充和图表中的线条的56种颜色的调色板。
使用 Aspose.Cells 不仅可以使用调色板的现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,首先将其添加到调色板中。
本主题讨论如何向调色板中添加自定义颜色。
向调色板添加自定义颜色
Aspose.Cells 支持 Microsoft Excel 的 56 种颜色调色板。要使用在调色板中未定义的自定义颜色,需要将颜色添加到调色板中。
Aspose.Cells 提供了一个名为 Workbook 的类,用于表示 Microsoft Excel 文件。 Workbook 类提供了一个 ChangePalette 方法,接受以下参数来添加自定义颜色以修改调色板:
- Custom Color,要添加的自定义颜色。
- Index,自定义颜色在调色板中的索引,将替换指定的颜色。应该在 0-55 之间。
下面的示例在应用于字体之前向调色板中添加了自定义颜色(兰花紫)。
// Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Checks if a color is in the palette for the spreadsheet. | |
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid)); | |
// Adding Orchid color to the palette at 55th index | |
workbook.ChangePalette(Color.Orchid, 55); | |
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid)); | |
// Adding a new worksheet to the Excel object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Defining new Style object | |
Style styleObject = workbook.CreateStyle(); | |
// Setting the Orchid (custom) color to the font | |
styleObject.Font.Color = workbook.Colors[55]; | |
// Applying the style to the cell | |
cell.SetStyle(styleObject); | |
// Saving the Excel file | |
workbook.Save("out.xlsx"); |