"Color Palette from Image"
Bootstrap 4.1.1 Snippet by SOJITRA

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/color-thief/2.3.0/color-thief.min.js" type="text/javascript"></script> <div class="container"> <h1 class="font-weight-light text-center text-lg-left mt-4 mb-0">Color Palette from Image<small class="float-right">Click Image</small></h1> <hr class="mt-2 mb-5"> <div class="row text-center text-lg-left"> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> <div class="col-lg-3 col-md-4 col-6"> <div class="d-block"> <img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous"> </div> </div> </div> </div> <div class="modal" id="myModal"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <!-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Color Palette</h4> <button type="button" class="close" data-dismiss="modal">×</button> </div> <!-- Modal body --> <div class="modal-body"> <div class="row"> <div class="col-sm-4"> <div class="img"></div> </div> <div class="col-sm-8"> <div class="detail"></div> </div> </div> </div> </div> </div> </div>
.d-block { margin-bottom: 30px } .d-block:after { display: table; width: 100%; clear: both; content: ''; } .img-thumbnail { border: 0; border-radius: 0; } .color-palette h6 { margin: 15px 0; } .color-palette h6:first-child { margin-top: 0; } .color-palette h6:last-child { margin-bottom: 0; } .main-color { display: inline-block; margin: 0 auto; vertical-align: middle; border: 1px solid; border-radius: 30px; overflow: hidden; } .main-color .color-box { display: inline-block; height: 60px; width: 60px; vertical-align: middle; } .main-color p { display: inline-block; vertical-align: middle; margin: 0 10px; } .palette-list { padding: 0; list-style: none; margin: 0 -5px; text-align: justify; } .palette-list:after { content: ''; display: inline-block; width: 100%; clear: both; } .palette-list .palette { margin: 5px; display: block; width: calc(33.333% - 10px); float: left; } .palette-list .palette .color-link { display: block; line-height: 40px; text-align: center; border: 1px solid; text-decoration: none; overflow: hidden; border-radius: 20px; } .palette-list .palette .color-link .color-box { height: 40px; width: 40px; display: block; vertical-align: middle; float: left; } .palette-list .palette .color-link p { display: block; margin: 0 10px; }
const afterRisizeListenersList = []; function triggerAfterResizeListeners() { afterRisizeListenersList.forEach(function(o) { if (typeof o.callBack === "function") { o.callBack.apply(null, o.arguments); } }) } /**/ var unsplash = ''; var colorThief = new ColorThief(); var imgs = document.querySelectorAll('img'); $.ajax({ url: "https://api.unsplash.com/photos/?&client_id=f0afe99aaf46a21c572d5dd3d620f3cf96e287a52e8af381f4bbaa5729a02215", success: function(result) { unsplash = result; $('img').each(function(i) { $(this).attr('src', unsplash[i].urls.small); }); $("img").one("load", function() { //colorPalette(this); var c = colorThief.getColor(this); $(this).css('background',rgbToHex(c[0], c[1], c[2])); $(this).on('click', function() { var modal = $('#myModal'); paletteModal(this, modal); }) }).each(function(i) { afterRisizeListenersList.push({ callBack: function() { colorPalette(this); }, arguments: [] }); if (this.complete) { // $(this).load(); // For jQuery < 3.0 $(this).trigger('load'); // For jQuery >= 3.0 } }); } }); function componentToHex(c) { var hex = c.toString(16); return hex.length == 1 ? "0" + hex : hex; } function rgbToHex(r, g, b) { return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); } function colorPalette(i) { var c = colorThief.getColor(i); var p = colorThief.getPalette(i, 10); i.style.borderColor = rgbToHex(c[0], c[1], c[2]); var node = document.createElement("DIV"); // Create a <div> node $(i).parent('.d-block').append('<div class="color-palette"><h6>Dominant Color</h6><div class="main-color" title="Copy to Clipboard"></div></div>'); $(i).parent('.d-block').find('.main-color').css('background-color', rgbToHex(c[0], c[1], c[2])); $(i).parent('.d-block').append('<h6>Palette</h6><ul class="palette-list"></ul>') for (var j = 0, m = p.length; j < m; j++) { var color = rgbToHex(p[j][0], p[j][1], p[j][2]); var t = $(i).parents('.d-block').find('ul.palette-list').append('<li class="palette" style="background-color: ' + color + '"><a href="#" onclick="return copyColor(this);" data-color="' + color + '" class="color-link" style="color:' + color + ';"><i class="fa fa-files-o"></i></a></li>'); } } function paletteModal(i, modal) { var img = modal.find('.modal-body .img').empty(); var detail = modal.find('.modal-body .detail').empty(); var c = colorThief.getColor(i); var p = colorThief.getPalette(i, 20); var color = rgbToHex(c[0], c[1], c[2]); img.append('<img class="img-fluid img-thumbnail" alt="" crossOrigin="anonymous">'); img.find('img').attr('src', $(i).attr('src')); detail.append('<div class="color-palette"><h6>Dominant Color</h6><a class="main-color" href="#" onclick="return copyColor(this);" data-color="' + color + '" style="color:' + color + ';"><div class="color-box" style="background:' + color + ';"></div><p><i class="fa fa-files-o"></i> <b>' + color + '</b></p></a></div>'); detail.find('.main-color ').css('border-color', color); detail.find('.color-palette').append('<h6>Palette</h6><ul class="palette-list"></ul>') for (var j = 0, m = p.length; j < m; j++) { var color = rgbToHex(p[j][0], p[j][1], p[j][2]); var t = modal.find('.modal-body ul.palette-list').append('<li class="palette"><a class="color-link" href="#" onclick="return copyColor(this);" data-color="' + color + '" style="color:' + color + '; border-color:' + color + ';"><div class="color-box" style="background:' + color + ';"></div><p><i class="fa fa-files-o"></i> <b>' + color + '</b></p></a></li>'); } modal.modal({ show: true }) } function copyColor(i) { var c = $(i).data('color'); var $temp = $("<input>"); $("body").append($temp); $temp.val(c).select(); document.execCommand("copy"); $temp.remove(); return false; }

Related: See More


Questions / Comments: