Skip to content

Instantly share code, notes, and snippets.

@Evgenus
Created August 23, 2011 09:27
Show Gist options
  • Save Evgenus/1164734 to your computer and use it in GitHub Desktop.
Save Evgenus/1164734 to your computer and use it in GitHub Desktop.
some natural color mixer
// Paint Colour Mixer
// copyright Stephen Chapman, 18th April 2006
// you may copy this code but please keep the copyright notice as well
var baseC = '256,256,256';
var redC = '-2,-8,-8';
var blueC = '-8,-8,-2';
var yellowC = '0,0,-8';
var orangeC = '1,1,0';
var greenC = '-4,0,-3';
var purpleC = '1,0,1';
var blackC = '-3,-4,-1';
var redV = blueV = yellowV = 0;
var totC = new Array(0, 0, 0);
function change(r, y, b) {
totC[0] += r;
totC[1] += y;
totC[2] += b;
var t = Math.max(totC[0], totC[1], totC[2]);
if (t > 32) t = 32 / t;
else t = 1;
redV = Math.round(totC[0] * t);
yellowV = Math.round(totC[1] * t);
blueV = Math.round(totC[2] * t);
colour();
}
function colour() {
document.getElementById('rd4').innerHTML = totC[0] / 32;
document.getElementById('bl4').innerHTML = totC[2] / 32;
document.getElementById('ye4').innerHTML = totC[1] / 32;
var shade = new mathArray(baseC);
var tot = redV + blueV + yellowV;
if (tot > 0) {
var redA = new mathArray(redC);
redA.multiply(redV * redV / tot);
shade.add(redA);
var blueA = new mathArray(blueC);
blueA.multiply(blueV * blueV / tot);
shade.add(blueA);
var yellowA = new mathArray(yellowC);
yellowA.multiply(yellowV * yellowV / tot);
shade.add(yellowA);
var orangeA = new mathArray(orangeC);
orangeA.multiply((redV * yellowV) / 32);
shade.add(orangeA);
var greenA = new mathArray(greenC);
greenA.multiply((blueV * yellowV) / 32);
shade.add(greenA);
var purpleA = new mathArray(purpleC);
purpleA.multiply((blueV * redV) / 32);
shade.add(purpleA);
var blackA = new mathArray(blackC);
blackA.multiply((blueV * redV * yellowV) / 1024);
shade.add(blackA);
}
shade.max(255);
shade.min(0);
shade.int();
document.getElementById('patch').style.backgroundColor = 'rgb(' + shade.value[0] + ',' + shade.value[1] + ',' + shade.value[2] + ')';
}
function set() {
totC[0] = totC[1] = totC[2] = redV = yellowV = blueV = 0;
colour();
}
// mathArray Class
// copyright Stephen Chapman, 13th April 2006
// you may copy this code but please keep the copyright notice as well
function mathArray(vals) {
var temp = vals.split(',');
var len = temp.length;
this.value = [];
for (var i = 0; i < len; i++) {
if (Number(temp[i]) == temp[i]) this.value.push(Number(temp[i]));
}
}
mathArray.prototype.add = function (mA) {
var len = Math.min(this.value.length, mA.value.length);
for (var i = 0; i < len; i++) {
this.value[i] += mA.value[i];
}
};
mathArray.prototype.subtract = function (mA) {
var len = Math.min(this.value.length, mA.value.length);
for (var i = 0; i < len; i++) {
this.value[i] -= mA.value[i];
}
};
mathArray.prototype.multiply = function (num) {
for (var i = this.value.length - 1; i >= 0; i--) {
this.value[i] *= num;
}
};
mathArray.prototype.replace = function (mA) {
this.value = '';
var len = mA.value.length;
for (var i = 0; i < len; i++) {
this.value[i] = mA.value[i];
}
};
mathArray.prototype.equal = function (mA) {
var len = mA.value.length;
if (this.value.length != len) return false;
for (var i = 0; i < len; i++) {
if (this.value[i] != mA.value[i]) return false;
}
return true;
};
mathArray.prototype.max = function (num) {
for (var i = this.value.length - 1; i >= 0; i--) {
if (this.value[i] > num) this.value[i] = num;
}
};
mathArray.prototype.min = function (num) {
for (var i = this.value.length - 1; i >= 0; i--) {
if (this.value[i] < num) this.value[i] = num;
}
};
mathArray.prototype.int = function () {
for (var i = this.value.length - 1; i >= 0; i--) {
this.value[i] = Math.round(this.value[i]);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment