Jump to content

User:Alex 21/script-functions.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 12:42, 2 March 2016 (Create.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function colourCompliance(bground,fground){
	var ratio;

	if (fground === false) {
        var ratio_w = getColorsRatio(bground,'FFFFFF');
        var ratio_b = getColorsRatio(bground,'000000');
        fground = (ratio_w > ratio_b ? 'FFFFFF' : '000000');
        ratio = (ratio_w > ratio_b ? ratio_w : ratio_b);
	} else {
		ratio = getColorsRatio(bground,fground);
	}

	if (ratio <= 7) {
		hsv = RGBtoHSV(br,bg,bb);
		bh = hsv.h; bs = hsv.s; bv = hsv.v;
		if (bv < 100 && bv > 0) {
			bv += (fground == 'FFFFFF' ? -1 : 1);
		} else if (bs < 100 && bs > 0) {
			bs += (fground == 'FFFFFF' ? 1 : -1);
		}
		var new_bground = HSVtoRGB(bh,bs,bv);
		new_bground = RGBtoHEX(new_bground[0],new_bground[1],new_bground[2]);
		return colourCompliance(new_bground,fground,false);
	} else {
		if (bground.length == 3) bground = bground[0]+bground[0]+bground[1]+bground[1]+bground[2]+bground[2];
		return bground.toUpperCase();
	}
}

function getColorsRatio(bground,fground) {
    var bgroundH = HEXtoRGB(bground);
    var fgroundH = HEXtoRGB(fground);
    
	br = bgroundH.r; bg = bgroundH.g; bb = bgroundH.b;
    fr = fgroundH.r; fg = fgroundH.g; fb = fgroundH.b;
	
	var ratio = 1;
	var l1 = getLuminance([fr/255, fg/255, fb/255]);
	var l2 = getLuminance([br/255, bg/255, bb/255]);

	if (l1 >= l2) {
		ratio = (l1 + 0.05) / (l2 + 0.05);
	} else {
		ratio = (l2 + 0.05) / (l1 + 0.05);
	}
	ratio = Math.round(ratio * 100) / 100;
	
	return ratio;
}

function getLuminance(rgb){
	for (var i = 0; i < rgb.length; i++) {
		if (rgb[i] <= 0.03928) {
			rgb[i] = rgb[i] / 12.92;	
		} else {
			rgb[i] = Math.pow( ((rgb[i]+0.055)/1.055), 2.4 );
		}
	}
	var l = (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]);
	return l;
}

function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function RGBtoHEX(r, g, b) {
    return componentToHex(r) + componentToHex(g) + componentToHex(b);
}

function HEXtoRGB(hex) {
	if (hex.length == 6) {
		return {
			r: parseInt(hex[0]+''+hex[1], 16),
			g: parseInt(hex[2]+''+hex[3], 16),
			b: parseInt(hex[4]+''+hex[5], 16)
		};
	} else {
		return {
			r: parseInt(hex[0]+''+hex[0], 16),
			g: parseInt(hex[1]+''+hex[1], 16),
			b: parseInt(hex[2]+''+hex[2], 16)
		};
	}
}

function RGBtoHSV() {
    var rr, gg, bb,
        r = arguments[0] / 255,
        g = arguments[1] / 255,
        b = arguments[2] / 255,
        h, s,
        v = Math.max(r, g, b),
        diff = v - Math.min(r, g, b),
        diffc = function(c){
            return (v - c) / 6 / diff + 1 / 2;
        };

    if (diff === 0) {
        h = s = 0;
    } else {
        s = diff / v;
        rr = diffc(r);
        gg = diffc(g);
        bb = diffc(b);

        if (r === v) {
            h = bb - gg;
        }else if (g === v) {
            h = (1 / 3) + rr - bb;
        }else if (b === v) {
            h = (2 / 3) + gg - rr;
        }
        if (h < 0) {
            h += 1;
        }else if (h > 1) {
            h -= 1;
        }
    }
    return {
        h: Math.round(h * 360),
        s: Math.round(s * 100),
        v: Math.round(v * 100)
    };
}

function HSVtoRGB(h,s,v) {
    s = s / 100,
	v = v / 100;

    var hi = Math.floor((h/60) % 6);
    var f = (h / 60) - hi;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);

    var rgb = [];

    switch (hi) {
        case 0: rgb = [v,t,p];break;
        case 1: rgb = [q,v,p];break;
        case 2: rgb = [p,v,t];break;
        case 3: rgb = [p,q,v];break;
        case 4: rgb = [t,p,v];break;
        case 5: rgb = [v,p,q];break;
    }

    var r = Math.min(255, Math.round(rgb[0]*256)),
        g = Math.min(255, Math.round(rgb[1]*256)),
        b = Math.min(255, Math.round(rgb[2]*256));

    return [r,g,b];

}