Added Color class

- Added Color class to make color representation and convertions more uniform
- Replaced standard convertions with Color.convertion
- Removed _algorithms.js, _deleteColor.js, ajax.js and the other convertion files
- Fixed bug in ColorModule, moved replaceAllOfColor to ColorModule as well as deleteColor
This commit is contained in:
unsettledgames
2021-07-11 12:40:48 +02:00
parent dbffc0b9da
commit f5807417ec
17 changed files with 367 additions and 643 deletions

View File

@@ -1,79 +0,0 @@
//GET: ajax(String url, Function success [,timeout])
//POST: ajax(String url, Object postData, Function success [,timeout])
Util.ajax = function (url, arg2, arg3, arg4) {
if (typeof arg2 == 'function') {
var success = arg2;
var timeout = arg3 || 10000;
}
else {
var postData = arg2;
var success = arg3;
var timeout = arg4 || 10000;
}
var start = new Date();
console.log('AJAX - STARTING REQUEST', url, '(' + timeout + ')');
//start new request
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var result;
//try to parse as json
try {
result = JSON.parse(this.response);
console.log('AJAX - COMPLETE ('+(new Date()-start)+'ms) - json:', result);
}
catch (e) {
result = this.response;
console.log('AJAX - COMPLETE ('+(new Date()-start)+'ms) - string:', this.response, e);
}
//return result
success(result);
xhr = null;
}
else if (this.readyState == 4) {
console.log('ajax failed', this.readyState, this.status);
success({ error: 'failure' });
}
};
xhr.ontimeout = function(e) {
console.log('ajax request timed out')
success({ error: 'timeout' });
};
if (postData) {
//post request
console.log('post data: ', postData instanceof FormData, postData);
//the the input isn't already formdata, convert it to form data
if (!(postData instanceof FormData)) {
console.log('converting to form data');
var formData = new FormData();
for (var key in postData) {
formData.append(key, postData[key]);
}
postData = formData;
}
//send to server
xhr.open("POST", url, true);
xhr.timeout = timeout;
xhr.send(postData);
}
else {
//get request
xhr.open("GET", url, true);
xhr.timeout = timeout;
xhr.send();
}
return xhr;
}

View File

@@ -1,21 +0,0 @@
//put in a hex color code (f464b2 or #f464b2) string
//and get an rgb color object {r:0,g:0,b:0}
//divisor is an optional argument, which makes it so you can get values other than 0-255
function hexToRgb(hex, divisor) {
//if divisor isn't set, set it to one (so it has no effect)
divisor = divisor || 1;
//split given hex code into array of 3 values
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex.trim());
//console.log('hex: '+hex)
//console.log([parseInt(result[1], 16)/divisor, parseInt(result[2], 16)/divisor, parseInt(result[3], 16)/divisor])
//console.log(result)
return result ? {
r: parseInt(result[1], 16)/divisor,
g: parseInt(result[2], 16)/divisor,
b: parseInt(result[3], 16)/divisor
} : null;
}

View File

@@ -1,32 +0,0 @@
function hslToRgb(h, s, l){
h /= 255;
s /= 255;
l /= 255;
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return {
r:Math.round(r * 255),
g:Math.round(g * 255),
b:Math.round(b * 255)
};
}

View File

@@ -1,23 +0,0 @@
//convert rgb values to a hex string for html
function rgbToHex (argument0,g,b) {
var r;
//if the first argument is an object
if (typeof argument0 === 'object'){
r = argument0.r;
g = argument0.g;
b = argument0.b;
}
else
r = argument0;
//console.log('converting rgb('+r+','+g+','+b+') to hex');
//convert a decimal number to 2-digit hex
function componentToHex (c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
return componentToHex(r) + componentToHex(g) + componentToHex(b);
}

View File

@@ -1,36 +0,0 @@
//put in red green blue values and get out hue saturation luminosity values
function rgbToHsl(argument0, g, b){
var r;
//if the first argument is an object
if (typeof argument0 === 'object'){
r = argument0.r;
g = argument0.g;
b = argument0.b;
}
else
r = argument0;
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var hue, saturation, luminosity = (max + min) / 2;
if(max == min){
hue = saturation = 0; // achromatic
}else{
var d = max - min;
saturation = luminosity > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: hue = (g - b) / d + (g < b ? 6 : 0); break;
case g: hue = (b - r) / d + 2; break;
case b: hue = (r - g) / d + 4; break;
}
hue /= 6;
}
return {h:hue, s:saturation, l:luminosity};
}