//Fill the gradient array with gray color.
var PaletteColors = Array.apply(null, Array(16)).map(function(item, i) {
return { id: i, color: { red: 128, green: 128, blue: 128 }, edited: false }; });
var ActiveBox = 0;
MakeGradient(); //run gradient maker at start
//Remove marker
$('.ColorPreview').contextmenu(function() {
$('#' + $(this).attr('id')).css('border-bottom', '0px');
return false
});
//Add marker and snap sliders to rgb values.
$('.ColorPreview').on('click', function() {
ActiveBox = Number($(this).data('col'));
$('#Color_' + ActiveBox).css('border-bottom', '6px solid');
$('#red').val(PaletteColors[ActiveBox].color.red);
$('#green').val(PaletteColors[ActiveBox].color.green);
$('#blue').val(PaletteColors[ActiveBox].color.blue);
var a = PaletteColors.filter(function(col, i) {
return col.edited || i === 0 || i === 15;
}).map(function(item, i) {
item.count = i;
return item;
});
});
//Read values from sldier write them to PaletteColors array.
$('.Paletteslider').on('change mousemove', function() {
var red = Number($('#red').val());
var green = Number($('#green').val());
var blue = Number($('#blue').val());
if (!PaletteColors[ActiveBox].edited) {
PaletteColors[ActiveBox].edited = true;
}
PaletteColors[ActiveBox].color.red = red;
PaletteColors[ActiveBox].color.green = green;
PaletteColors[ActiveBox].color.blue = blue;
MakeGradient();
});
//Draw gradient based on PaletteColors array.
function MakeGradient() {
var colors = PaletteColors.filter(function(col, i) {
return col.edited || i === 0 || i === 15;
}).map(function(item, i) {
item.count = i;
return item;
});
var FirstColor = colors[0];
var SecondColor = colors[1];
var end = 15;
var Count = 1 / end;
for (i = 0; i <= end; i++) {
if (colors.length > 2 && i !== 0 && i !== 15 && SecondColor.id === i) {
FirstColor = SecondColor;
SecondColor = colors[SecondColor.count + 1];
}
var Step = Count * i;
var Current = {};
var str;
var match = FirstColor.id === i || SecondColor.id === i ? FirstColor.id === i ? 1 : 2 : false;
if (match) {
Current = match === 1 ? FirstColor.color : SecondColor.color;
} else {
Current.red = Math.round(SecondColor.color.red * Step + FirstColor.color.red * (1 - Step));
Current.green = Math.round(SecondColor.color.green * Step + FirstColor.color.green * (1 - Step));
Current.blue = Math.round(SecondColor.color.blue * Step + FirstColor.color.blue * (1 - Step));
}
PaletteColors[i].color.red = Current.red > 255 ? 255 : Current.red;
PaletteColors[i].color.green = Current.green > 255 ? 255 : Current.green;
PaletteColors[i].color.blue = Current.blue > 255 ? 255 : Current.blue;
str = 'rgb(' + [Current.red, Current.blue, Current.green].join(',') + ')';
$('#Color_' + i).css('background-color', str);
}
}
body, html{
background: #222222;
font-family: 'Lato', sans-serif;
color:white;
}
.ColorPreview {
background: rgb(0,0,0);
float: left;
height: 5em;
width: 6.25%;
border-bottom: 0px solid;
border-color: lightgrey;
}
#Color_0 {border-bottom: 0px solid;}
#Color_1 {border-bottom: 0px solid;}
#Color_2 {border-bottom: 0px solid;}
#Color_3 {border-bottom: 0px solid;}
#Color_4 {border-bottom: 0px solid;}
#Color_5 {border-bottom: 0px solid;}
#Color_6 {border-bottom: 0px solid;}
#Color_7 {border-bottom: 0px solid;}
#Color_8 {border-bottom: 0px solid;}
#Color_9 {border-bottom: 0px solid;}
#Color_10 {border-bottom: 0px solid;}
#Color_11 {border-bottom: 0px solid;}
#Color_12 {border-bottom: 0px solid;}
#Color_13 {border-bottom: 0px solid;}
#Color_14 {border-bottom: 0px solid;}
#Color_15 {border-bottom: 0px solid;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="PreviewTable" >
<div class="ColorPreview" id="Color_0" data-col="0">Color0</div>
<div class="ColorPreview" id="Color_1" data-col="1">Color1</div>
<div class="ColorPreview" id="Color_2" data-col="2">Color2</div>
<div class="ColorPreview" id="Color_3" data-col="3">Color3</div>
<div class="ColorPreview" id="Color_4" data-col="4">Color4</div>
<div class="ColorPreview" id="Color_5" data-col="5">Color5</div>
<div class="ColorPreview" id="Color_6" data-col="6">Color6</div>
<div class="ColorPreview" id="Color_7" data-col="7">Color7</div>
<div class="ColorPreview" id="Color_8" data-col="8">Color8</div>
<div class="ColorPreview" id="Color_9" data-col="9">Color9</div>
<div class="ColorPreview" id="Color_10" data-col="10">Color10</div>
<div class="ColorPreview" id="Color_11" data-col="11">Color11</div>
<div class="ColorPreview" id="Color_12" data-col="12">Color12</div>
<div class="ColorPreview" id="Color_13" data-col="13">Color13</div>
<div class="ColorPreview" id="Color_14" data-col="14">Color14</div>
<div class="ColorPreview" id="Color_15" data-col="15">Color15</div>
</div>
<div>
</div>
<div>
<p>
right click to remove marker
</p>
</div>
<div>
<p>red</p>
<span id="spanHueIndex"></span>
<p> <input class="slider Paletteslider" id="red" type="range" step="1" min="0" max="255" value="0" /></p>
<p>green</p>
<span id="spanSaturationIndex"></span>
<p> <input class="slider Paletteslider" id="green" type="range" step="1" min="0" max="255" value="0" /></p>
<p>blue</p>
<span id="spanLightnessIndex"></span>
<p> <input class="slider Paletteslider" id="blue" type="range" step="1" min="0" max="255" value="0" /></p>
</div>