div {
width:50px;
height:50px;
margin:0 0 0 20px;
}
p {
margin:20px;
}
JavaScript
$(document).ready(function(){
function getSimilarColors (color) {
var base_colors=["#000000", " #1F95CE", " #237A91", " #2C9AB7", " #373737", " #449A88", " #484848", " #52BAD5", " #595959", " #5CDDCA", " #626262", " #6BC467", " #6DC5DC", " #72C1B0", " #737373", " #848484", " #91A4E1", " #959595", " #95D1C4", " #B1E0EC", " #B7B7B7", " #C5E5DE", " #CCEBF3", " #D0D0D0", " #D4ECE6", " #D6B250", " #D9D9D9", " #DB3A1B", " #E0E0E0", " #E85C41", " #EE836E", " #F2A56D", " #F2F2F2", " #F5B7AB", " #F8D0C8", " #F8F8F8", " #FEBE12", " #FED156", " #FEDE88", " #FFE8AA", " #FFEEBF", " #FFFFFF"];
//Convert to RGB, then R, G, B
var color_rgb = hex2rgb(color);
var color_r = color_rgb.split(',')[0];
var color_g = color_rgb.split(',')[1];
var color_b = color_rgb.split(',')[2];
//Create an emtyp array for the difference betwwen the colors
var differenceArray=[];
//Function to find the smallest value in an array
Array.min = function( array ){
return Math.min.apply( Math, array );
};
//Convert the HEX color in the array to RGB colors, split them up to R-G-B, then find out the difference between the "color" and the colors in the array
$.each(base_colors, function(index, value) {
var base_color_rgb = hex2rgb(value);
var base_colors_r = base_color_rgb.split(',')[0];
var base_colors_g = base_color_rgb.split(',')[1];
var base_colors_b = base_color_rgb.split(',')[2];
//Add the difference to the differenceArray
differenceArray.push(Math.sqrt((color_r-base_colors_r)*(color_r-base_colors_r)+(color_g-base_colors_g)*(color_g-base_colors_g)+(color_b-base_colors_b)*(color_b-base_colors_b)));
});
//Get the lowest number from the differenceArray
var lowest = Array.min(differenceArray);
//Get the index for...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.