From 5dbf215c9c9c733b442e66cbfcadeee077364287 Mon Sep 17 00:00:00 2001 From: Pieter Sheth-Voss Date: Fri, 17 Apr 2015 13:15:13 -0400 Subject: [PATCH] Correct hex2RGB coding, apply tints --- bits/45_styutils.js | 7 ++++--- bits/47_styxml.js | 8 ++++---- test.js | 33 ++++++++++++++++----------------- xlsx.js | 15 ++++++++------- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/bits/45_styutils.js b/bits/45_styutils.js index 16d30bc..99c9908 100644 --- a/bits/45_styutils.js +++ b/bits/45_styutils.js @@ -1,6 +1,6 @@ function hex2RGB(h) { var o = h.substr(h[0]==="#"?1:0,6); - return [parseInt(o.substr(0,2),16),parseInt(o.substr(0,2),16),parseInt(o.substr(0,2),16)]; + return [parseInt(o.substr(0,2),16),parseInt(o.substr(2,2),16),parseInt(o.substr(4,2),16)]; } function rgb2Hex(rgb) { for(var i=0,o=1; i!=3; ++i) o = o*256 + (rgb[i]>255?255:rgb[i]<0?0:rgb[i]); @@ -42,11 +42,12 @@ function hsl2RGB(hsl){ /* 18.8.3 bgColor tint algorithm */ function rgb_tint(hex, tint) { - if(tint === 0) return hex; + if(tint == 0) return hex; var hsl = rgb2HSL(hex2RGB(hex)); if (tint < 0) hsl[2] = hsl[2] * (1 + tint); else hsl[2] = 1 - (1 - hsl[2]) * (1 - tint); - return rgb2Hex(hsl2RGB(hsl)); + var rev =rgb2Hex(hsl2RGB(hsl)) + return rev; } /* 18.3.1.13 width calculations */ diff --git a/bits/47_styxml.js b/bits/47_styxml.js index b016322..f7af92c 100644 --- a/bits/47_styxml.js +++ b/bits/47_styxml.js @@ -35,8 +35,8 @@ function parse_fills(t, opts) { if (y.theme && themes.themeElements && themes.themeElements.clrScheme) { - fill.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, 0) ; //fill.fgColor.tint || 0); - if (opts.WTF) fill.bgColor.raw_rgb = fill.bgColor.rgb; + fill.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, fill.bgColor.tint || 0); + if (opts.WTF) fill.bgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb,0); } /* Excel uses ARGB strings */ if (y.rgb) fill.bgColor.rgb = y.rgb;//.substring(y.rgb.length - 6); @@ -52,8 +52,8 @@ function parse_fills(t, opts) { if (y.tint) fill.fgColor.tint = parseFloat(y.tint); if (y.theme && themes.themeElements && themes.themeElements.clrScheme) { - fill.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, 0); //fill.fgColor.tint || 0); - if (opts.WTF) fill.fgColor.raw_rgb = fill.fgColor.rgb; + fill.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, fill.fgColor.tint || 0); + if (opts.WTF) fill.fgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb,0); } /* Excel uses ARGB strings */ diff --git a/test.js b/test.js index 970a9b1..296bcb0 100644 --- a/test.js +++ b/test.js @@ -766,32 +766,31 @@ describe('parse features', function() { 'H1:J4', 'H10' /* blocks */ ]; - var exp = [ { patternType: 'darkHorizontal', - fgColor: { theme: 9, "tint":-0.249977111117893, rgb: 'F79646' }, - bgColor: { theme: 5, "tint":0.3999755851924192, rgb: 'C0504D' } }, + fgColor: { theme: 9, "tint":-0.249977111117893, rgb_raw: 'F79646', rgb: "E46C0A"}, + bgColor: { theme: 5, "tint":0.3999755851924192, rgb_raw: 'C0504D', rgb: "D99694" } }, { patternType: 'darkUp', - fgColor: { theme: 3, "tint":-0.249977111117893, rgb: 'EEECE1' }, - bgColor: { theme: 7, "tint":0.3999755851924192, rgb: '8064A2' } }, + fgColor: { theme: 3, "tint":-0.249977111117893, rgb_raw: 'EEECE1', rgb: "C4BD97" }, + bgColor: { theme: 7, "tint":0.3999755851924192, rgb_raw: '8064A2', rgb: "B3A2C7" } }, { patternType: 'darkGray', - fgColor: { theme: 3, rgb: 'EEECE1' }, - bgColor: { theme: 1, rgb: 'FFFFFF' } }, + fgColor: { theme: 3, rgb_raw: 'EEECE1', rgb: "EEECE1" }, + bgColor: { theme: 1, rgb_raw: 'FFFFFF', rgb: "FFFFFF" } }, { patternType: 'lightGray', - fgColor: { theme: 6, "tint":0.3999755851924192, rgb: '9BBB59' }, - bgColor: { theme: 2, "tint":-0.499984740745262, rgb: '1F497D' } }, + fgColor: { theme: 6, "tint":0.3999755851924192, rgb_raw: '9BBB59', rgb: "C3D69B" }, + bgColor: { theme: 2, "tint":-0.499984740745262, rgb_raw: '1F497D', rgb: "10253F" } }, { patternType: 'lightDown', - fgColor: { theme: 4, rgb: '4F81BD' }, - bgColor: { theme: 7, rgb: '8064A2' } }, + fgColor: { theme: 4, "tint":-0.249977111117893, rgb_raw: '4F81BD', rgb: "376092" }, + bgColor: { theme: 7, "tint":-0.249977111117893, rgb_raw: '8064A2', rgb: "604A7B" } }, { patternType: 'lightGrid', - fgColor: { theme: 6, "tint":-0.249977111117893, rgb: '9BBB59' }, - bgColor: { theme: 9, "tint":-0.249977111117893, rgb: 'F79646' } }, + fgColor: { theme: 6, "tint":-0.249977111117893, rgb_raw: '9BBB59', rgb: "77933C" }, + bgColor: { theme: 9, "tint":-0.249977111117893, rgb_raw: 'F79646', rgb: "E46C0A" } }, { patternType: 'lightGrid', - fgColor: { theme: 4, rgb: '4F81BD' }, - bgColor: { theme: 2, "tint":-0.749992370372631, rgb: '1F497D' } }, + fgColor: { theme: 4, rgb_raw: '4F81BD' , rgb: "4F81BD"}, + bgColor: { theme: 2, "tint":-0.749992370372631, rgb_raw: '1F497D', rgb: "08121F" } }, { patternType: 'lightVertical', - fgColor: { theme: 3, "tint":0.3999755851924192, rgb: 'EEECE1' }, - bgColor: { theme: 7, "tint":0.3999755851924192, rgb: '8064A2' } } + fgColor: { theme: 3, "tint":0.3999755851924192, rgb_raw: 'EEECE1', rgb: "F5F4ED" }, + bgColor: { theme: 7, "tint":0.3999755851924192, rgb_raw: '8064A2', rgb: "B3A2C7" } } ]; ranges.forEach(function(rng) { it('XLS | ' + rng,function(){cmparr(rn2(rng).map(function(x){ return wsxls[x].s; }));}); diff --git a/xlsx.js b/xlsx.js index 3bcd52b..579b9c3 100644 --- a/xlsx.js +++ b/xlsx.js @@ -4548,7 +4548,7 @@ function parse_FilePass(blob, length, opts) { function hex2RGB(h) { var o = h.substr(h[0]==="#"?1:0,6); - return [parseInt(o.substr(0,2),16),parseInt(o.substr(0,2),16),parseInt(o.substr(0,2),16)]; + return [parseInt(o.substr(0,2),16),parseInt(o.substr(2,2),16),parseInt(o.substr(4,2),16)]; } function rgb2Hex(rgb) { for(var i=0,o=1; i!=3; ++i) o = o*256 + (rgb[i]>255?255:rgb[i]<0?0:rgb[i]); @@ -4590,11 +4590,12 @@ function hsl2RGB(hsl){ /* 18.8.3 bgColor tint algorithm */ function rgb_tint(hex, tint) { - if(tint === 0) return hex; + if(tint == 0) return hex; var hsl = rgb2HSL(hex2RGB(hex)); if (tint < 0) hsl[2] = hsl[2] * (1 + tint); else hsl[2] = 1 - (1 - hsl[2]) * (1 - tint); - return rgb2Hex(hsl2RGB(hsl)); + var rev =rgb2Hex(hsl2RGB(hsl)) + return rev; } /* 18.3.1.13 width calculations */ @@ -4671,8 +4672,8 @@ function parse_fills(t, opts) { if (y.theme && themes.themeElements && themes.themeElements.clrScheme) { - fill.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, 0) ; //fill.fgColor.tint || 0); - if (opts.WTF) fill.bgColor.raw_rgb = fill.bgColor.rgb; + fill.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, fill.bgColor.tint || 0); + if (opts.WTF) fill.bgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb,0); } /* Excel uses ARGB strings */ if (y.rgb) fill.bgColor.rgb = y.rgb;//.substring(y.rgb.length - 6); @@ -4688,8 +4689,8 @@ function parse_fills(t, opts) { if (y.tint) fill.fgColor.tint = parseFloat(y.tint); if (y.theme && themes.themeElements && themes.themeElements.clrScheme) { - fill.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, 0); //fill.fgColor.tint || 0); - if (opts.WTF) fill.fgColor.raw_rgb = fill.fgColor.rgb; + fill.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, fill.fgColor.tint || 0); + if (opts.WTF) fill.fgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb,0); } /* Excel uses ARGB strings */