2011-07-28 74 views
1

Xlslib提供了2種方法改變細胞的背景色:如何填充顏色與xlslib

void fillbgcolor(color_name_t color); 
void fillbgcolor(unsigned8_t color); 

但我怎麼可以使用自定義顏色(例如#EFEFEF)?

回答

0

我不知道color_name_tunsigned8_t的確切定義,但我想這些都是某種整數類型的typedef。如果是這種情況,可以使用十六進制整數表示法以所需的格式編寫顏色代碼。

編輯:您可以使用前綴0x寫入十六進制值,例如:0xEFEFEF。

EDIT2:擴展的例子

fillbgcolor(0xEFEFEF); 
+0

不,它不是那麼容易。 Excel使用調色板和修改顏色,我們可以使用:** workbook wb; wb.setColor(0xef,0xef,0xef,0x09); **(第一種顏色從0x09開始)並使用它:** cell-> fillfgcolor(0x09); ** – revo

0

您可以用十六進制值,下面的方法爲十六進制顏色值轉換成RGB值,並將其傳遞到DHWorkBook的setupNewColors設置自定義顏色,你必須使用DHxlsIOS SDK爲了那個原因。爲此,我創建了一個通用方法,其中傳遞DHWorkBook,DHCell,colourID和hex colorstring的對象,例如EFEFEF,CACACA。您可以傳遞9到63之間的任何數字作爲顏色標識,您可以在「color.h」文件中檢查顏色的標識。然後,您需要使用setupNewColors設置RGB顏色,並使用我創建的fillCGcolorUnsigned填充到單元格中,以通過unsigned8_t顏色對象。

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

我使用下面的方法另一種方法來從十六進制字符串獲取RGB值。您不需要使用像'#'或'0X'這樣的前綴,因爲它們在此方法中已經被截斷以轉換爲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]); 
} 

我還創建了兩個自定義的方法產生DhCell & cell.cpp,而不是通過color_name_t的unsigned8_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(); 
}