Skip to content

Instantly share code, notes, and snippets.

@mplungjan
mplungjan / demo.html
Created April 18, 2024 09:35
CIELAB get nearest colour
<title>Color Distance in CIELAB</title>
<div style="background:#520975; width:100px; height:100px; display:block; float:left; margin:0;">#520975<br>array color</div>
<div style="background:#5D0C8B; width:100px; height:100px; display:block; float:left; margin:0;">#5D0C8B<br>array color</div>
<div style="clear:both;background:#854B97; width:100px; height:100px; display:block; float:left; margin:0;">#854B97<br>find near for this color</div>
<div id="near" style="width:100px; height:100px; display:block; float:left; margin:0;"></div>
<script>
const colors = [
"#B2CFAD", "#F4C6CE", "#7CADD3", "#A4C7E2", "#1BCFC9", "#F7EA5F", "#ffffff", "#520975", "#004987", "#F5E500",
"#FF8300", "#D8262E", "#999899", "#016836", "#005CB8", "#008995", "#D08900", "#EF8F7A", "#F0B2C9", "#EA534D",
@mplungjan
mplungjan / gist:4461086
Created January 5, 2013 11:25
Test a date for validity by creating JS date object
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test a date string - by mplungjan</title>
<script>
function isValidDateCheck(dString) {
var dRe = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/
if (!dRe.exec(dString)) {
@mplungjan
mplungjan / gist:4461082
Created January 5, 2013 11:21
Filter example
var elemsToKeep = [1, 3, 5, 6, 8];
var arr=[];
arr[0] = 'one1';
arr[1] = 'two2'; // remove
arr[2] = 'three3';
arr[3] = 'four4'; // remove
arr[4] = 'five5';
arr[5] = 'six6';
arr[6] = 'seven7'; // remove
@mplungjan
mplungjan / gist:4461078
Created January 5, 2013 11:19
Loop with closure
<html>
<head>
<script>
// using bing since google does not like being inside a jsfiddle frame
var myLinks = [
'http://bing.com/search?q=go',
'http://bing.com/search?q=visit',
'http://bing.com/search?q=explore',
'http://bing.com/search?q=funny%20pics'
]; // note no comma on the last item
@mplungjan
mplungjan / gist:4460894
Created January 5, 2013 10:25
Cross browser rudimentary console
//if (!window.console) window.console={ log:function(str) { alert(str) } }
if (!window.console)
window.console={ log:function() {
var text = [];
for (var a,i=0;i<arguments.length;i++) {
a=arguments[i];
text.push(typeof a+": "+(typeof a == "object" ? a.toString():a));
}
alert(text.join('\n'))
}
@mplungjan
mplungjan / gist:4460865
Created January 5, 2013 10:18
Find unique characters in string - demo http://jsfiddle.net/mplungjan/YUXVF/
function find_unique_characters(string){
unique=[];
while(string.length>0){
var char = string.charAt(0);
var re = new RegExp(char,"g");
console.log(re,char,string.match(re))
if (string.match(re).length===1) unique.push(char);
string=string.replace(re,"");
}
return unique.join("");