How to use custom colors with xlslib

139 views Asked by At

I'm using xlsLib to build an Excel spreadsheet from data that's in a proprietary format. The data contains RGB information for coloring certain cells, and I want the resulting Excel file to reflect those custom colors.

xlsLib provides a method to set the color of a cell like this:

myCell->fillfgcolor(color_name_t);

and that works fine for the predefined colors defined in color_name_t. But how do I tell it to use my custom colors instead of the predefined ones?

It looks like I can create custom colors with:

myWorkbook->setColor(r, g, b, idx);

where idx is a value between 8 and 64. It appears setColor() stashes this custom color into a palette array for later use, but then cell::fillfgcolor() doesn't seem to use that palette.

What should I be calling instead of fillfgcolor() to set the color of a cell using my custom palette?

1

There are 1 answers

0
sajgan2015 On

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();
}