How fill color with xlslib
Xlslib provides 2 methods for changing the background color of a cell:
void fillbgcolor(color_name_t color);
void fillbgcolor(un开发者_StackOverflow中文版signed8_t color);
But how can I use a custom color (for example #EFEFEF)?
I do not know the exact definition for color_name_t
or unsigned8_t
but I guess these are typedefs of some integer type. If this is the case, you can use hexadecimal integer representation to write the color code in your desired format.
EDIT: You write hexadecimal values with the 0x
prefix, eg: 0xEFEFEF.
EDIT2: extended example
fillbgcolor(0xEFEFEF);
You can set custom color using hex value and following method to convert hex color value into RGB value and passing it into setupNewColors of DHWorkBook and you have to use DHxlsIOS SDK for that. For this, I created one general method in which i am passing object of DHWorkBook, DHCell, colourID and hex colorstring e.g EFEFEF, CACACA. You can pass any number between 9 to 63 as Color id which id for color which you can check in "color.h" file. And then you need set RGB color using setupNewColors and fill into cell using fillBGcolorUnsigned of DHCell which i created to pass unsigned8_t color object.
-(void)customColor:(DHWorkBook *)workbook cell:(DHCell *)cell customColorId:(unsigned int)customColorId hexString:(NSString *)hexString
{
unsigned int components[3];
[self rgbFromHexString:hexString
rgb:components];
[workbook setupNewColors:customColorId
red:components[0]
green:components[1]
blue:components[2]];
[cell fillBGcolorUnsigned:customColorId];
}
I use below method another method to get RGB value from hex string. You need not use prefix like '#' or '0X' because they are already truncated in this method for converting into RGB.
-(void)rgbFromHexString:(NSString*)hex rgb:(unsigned int [3])components // New
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6)
{
// Gray color
components[0] = 128; // r
components[1] = 128; // g
components[2] = 128; // b
}
// Truncate 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6)
{
// Gray color
components[0] = 128; // r
components[1] = 128; // g
components[2] = 128; // b
}
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r = 0, g = 0, b = 0;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
components[0] = r;
components[1] = g;
components[2] = b;
NSLog(@"r - %u g - %u b - %u",components[0],components[1],components[2]);
}
I also created two custom methods in DHCell & cell.cpp to pass unsigned8_t instead of color_name_t.
-(void)fillBGcolorUnsigned:(unsigned8_t)color
{
CELL(aCell)->fillbgcolor(color);
}
void cell_t::fillbgcolor(unsigned8_t color)
{
xf_t * tempXF = xf_t::xfDup(pxf);
tempXF->SetFillBGColor(color);
pxf->UnMarkUsed();
pxf = m_GlobalRecords.findXF(tempXF);
pxf->MarkUsed();
}
精彩评论