xsheetjs/bits/45_styutils.js

119 lines
3.8 KiB
JavaScript
Raw Normal View History

function hex2RGB(h) {
var o = h.substr(h[0]==="#"?1:0,6);
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]);
return o.toString(16).toUpperCase().substr(1);
}
function rgb2HSL(rgb) {
var R = rgb[0]/255, G = rgb[1]/255, B=rgb[2]/255;
var M = Math.max(R, G, B), m = Math.min(R, G, B), C = M - m;
if(C === 0) return [0, 0, R];
var H6 = 0, S = 0, L2 = (M + m);
S = C / (L2 > 1 ? 2 - L2 : L2);
switch(M){
case R: H6 = ((G - B) / C + 6)%6; break;
case G: H6 = ((B - R) / C + 2); break;
case B: H6 = ((R - G) / C + 4); break;
}
return [H6 / 6, S, L2 / 2];
}
function hsl2RGB(hsl){
var H = hsl[0], S = hsl[1], L = hsl[2];
var C = S * 2 * (L < 0.5 ? L : 1 - L), m = L - C/2;
var rgb = [m,m,m], h6 = 6*H;
var X;
if(S !== 0) switch(h6|0) {
case 0: case 6: X = C * h6; rgb[0] += C; rgb[1] += X; break;
case 1: X = C * (2 - h6); rgb[0] += X; rgb[1] += C; break;
case 2: X = C * (h6 - 2); rgb[1] += C; rgb[2] += X; break;
case 3: X = C * (4 - h6); rgb[1] += X; rgb[2] += C; break;
case 4: X = C * (h6 - 4); rgb[2] += C; rgb[0] += X; break;
case 5: X = C * (6 - h6); rgb[2] += X; rgb[0] += C; break;
}
for(var i = 0; i != 3; ++i) rgb[i] = Math.round(rgb[i]*255);
return rgb;
}
/* 18.8.3 bgColor tint algorithm */
function rgb_tint(hex, tint) {
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));
}
/* 18.3.1.13 width calculations */
/* [MS-OI29500] 2.1.595 Column Width & Formatting */
var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }
function px2char_(px) { return (((px - 5)/MDW * 100 + 0.5))/100; }
function char2width_(chr) { return (((chr * MDW + 5)/MDW*256))/256; }
function cycle_width(collw) { return char2width(px2char(width2px(collw))); }
/* XLSX/XLSB/XLS specify width in units of MDW */
function find_mdw_colw(collw) {
var delta = Infinity, _MDW = MIN_MDW;
for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) if(Math.abs(collw - cycle_width(collw)) <= delta) { delta = Math.abs(collw - cycle_width(collw)); _MDW = MDW; }
MDW = _MDW;
}
/* XLML specifies width in terms of pixels */
function find_mdw_wpx(wpx) {
var delta = Infinity, guess = 0, _MDW = MIN_MDW;
for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) {
guess = char2width_(px2char_(wpx))*256;
guess = (guess) % 1;
if(guess > 0.5) guess--;
if(Math.abs(guess) < delta) { delta = Math.abs(guess); _MDW = MDW; }
}
MDW = _MDW;
}
function process_col(coll/*:ColInfo*/) {
if(coll.width) {
coll.wpx = width2px(coll.width);
coll.wch = px2char(coll.wpx);
coll.MDW = MDW;
} else if(coll.wpx) {
coll.wch = px2char(coll.wpx);
coll.width = char2width(coll.wch);
coll.MDW = MDW;
} else if(typeof coll.wch == 'number') {
coll.width = char2width(coll.wch);
coll.wpx = width2px(coll.width);
coll.MDW = MDW;
}
if(coll.customWidth) delete coll.customWidth;
}
var DEF_PPI = 96, PPI = DEF_PPI;
function px2pt(px) { return px * 96 / PPI; }
function pt2px(pt) { return pt * PPI / 96; }
/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
var XLMLPatternTypeMap = {
"None": "none",
"Solid": "solid",
"Gray50": "mediumGray",
"Gray75": "darkGray",
"Gray25": "lightGray",
"HorzStripe": "darkHorizontal",
"VertStripe": "darkVertical",
"ReverseDiagStripe": "darkDown",
"DiagStripe": "darkUp",
"DiagCross": "darkGrid",
"ThickDiagCross": "darkTrellis",
"ThinHorzStripe": "lightHorizontal",
"ThinVertStripe": "lightVertical",
"ThinReverseDiagStripe": "lightDown",
"ThinHorzCross": "lightGrid"
};