UNPKG

color-scheme

Version:
373 lines (332 loc) 13.4 kB
<!DOCTYPE html> <html lang="en"></html> <head> <meta charset="utf-8"> <title>color-scheme.js</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="color-scheme.js - generate pleasant color schemes in JavaScript"> <meta name="author" content="Brian Hann"> <style> body { padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ } .color { height: 50px; width: 50px; float: left; margin: 5px; border: 1px solid #ccc; } #hue-box { margin-left: 20px; margin-top: -19px; } .bordered { border-top: 1px solid #ccc; } #hex-box { background-color: #fff; position: relative; top: -15px; left: -60px; } </style> <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> <link href="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet"> <!-- HTML5 shim, for IE6-8 support of HTML5 elements --><!--[if lt IE 9]> <script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script><![endif]--> </head> <body ng-app><a href="https://github.com/c0bra/color-scheme-js" class="hidden-phone"><img style="position: absolute; top: 0; right: 0; border: 0; z-index: 2000" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <button type="button" data-toggle="collapse" data-target=".nav-collapse" class="btn btn-navbar"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a href="#" class="brand">color-scheme.js</a> <div class="nav-collapse collapse"> <ul class="nav"> <li><a href="#example">Example</a></li> <li><a href="#tryit">Try it!</a></li> </ul> </div> </div> </div> </div> <div class="container"> <div class="row"> <div class="span12"> <div class="row"> <div class="span12"> <h1>color-scheme.js</h1> </div> </div> <div class="row"> <div class="span12"> <p>Generate nice, pretty color schemes with JavaScript.</p> </div> </div> </div> </div> <div class="row"> <div class="span12"> <h2 id="example">Example</h2> <pre>scm = new ColorScheme; scm.from_hue(21) .scheme('triade') .distance(0.1) .add_complement(false) .variation('pastel') .web_safe(true); var colors = scm.colors(); console.log(colors); # [ 'ff9966', '996633', 'ffcccc', 'cc6633', '669966', '339966', 'ccffcc', '33cc99', '339999', '339966', 'ccffcc', '33cc99' ] </pre> </div> </div> <div class="row"> <div class="span12"> <h2 id="tryit">Try it!</h2> </div> <div class="row"> <div class="span12"> <div class="offset1"> <div class="row"> <div class="span12"><strong class="span1 pull-left">Hue</strong> <div id="hue-slider" class="span5"></div> <div id="hue-box" style="background-color: #ffffff" class="color middle"></div> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span3"><strong class="span1 pull-left">Hex</strong> <div class="input-append"> <input id="hex" type="text" name="hex" maxlength="6" class="span1"> <button type="button" id="set-hex" class="btn">Set</button> </div> </div> <div class="span1"> <div id="hex-box" class="color middle"></div> <div class="clearfix"></div> </div> <div class="span4"><small>(Note: this functions a bit differently from setting the hue above; it will create a custom variation "preset" that differs from any of the below options)</small></div> </div> <div class="row bordered">&nbsp;</div> <div class="row"> <div class="span12"><strong class="span1">Scheme</strong> <div data-toggle="buttons-radio" class="btn-group"> <button type="button" data-toggle="button" onclick="setScheme(&quot;monochromatic&quot;)" class="btn active">Monochromatic</button> <button type="button" data-toggle="button" onclick="setScheme(&quot;contrast&quot;)" class="btn">Contrast</button> <button type="button" data-toggle="button" onclick="setScheme(&quot;triade&quot;)" class="btn">Triade</button> <button type="button" data-toggle="button" onclick="setScheme(&quot;tetrade&quot;)" class="btn">Tetrade</button> <button type="button" data-toggle="button" onclick="setScheme(&quot;analogic&quot;)" class="btn">Analogic</button> </div> <button id="add-complement" type="button" data-toggle="button" class="btn hide">Add Complement</button> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span12"><strong class="span1 pull-left">Distance</strong> <div id="distance-slider" class="span5"> </div><span id="distance"></span><small style="padding-left: 20px">(won't affect mono or contrast)</small> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span12"><strong class="span1 pull-left">Variation</strong> <div data-toggle="buttons-radio" class="btn-group"> <button type="button" data-toggle="button" onclick="setVariation(&quot;default&quot;)" class="btn active">Default</button> <button type="button" data-toggle="button" onclick="setVariation(&quot;pastel&quot;)" class="btn">Pastel</button> <button type="button" data-toggle="button" onclick="setVariation(&quot;soft&quot;)" class="btn">Soft</button> <button type="button" data-toggle="button" onclick="setVariation(&quot;light&quot;)" class="btn">Light</button> <button type="button" data-toggle="button" onclick="setVariation(&quot;hard&quot;)" class="btn">Hard</button> <button type="button" data-toggle="button" onclick="setVariation(&quot;pale&quot;)" class="btn">Pale</button> </div> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span12"><strong class="span1 pull-left">Web Safe</strong> <div data-toggle="buttons-radio" class="btn-group"> <button data-toggle="button" onclick="setWebSafe(true)" class="btn">Yes</button> <button data-toggle="button" onclick="setWebSafe(false)" class="btn active">No</button> </div> </div> </div> </div> </div> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span12"> <h4>Generated Colors</h4> </div> <div class="row span12"> <div id="colors"></div> <div class="clearfix"></div> </div> </div> <div class="row">&nbsp;</div> <div class="row"> <div class="span12"> <h3>Todo</h3> <p> The code is translated but all the docs and tests need to be written/copied. Hang tight! </p> </div> </div> </div> <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script> <script src="lib/color-scheme.js"></script> <script> //- console.log("C", ColorScheme); var scheme = null; $(document).ready(function(){ $("#hue-slider").slider({ min: 0, max: 360, slide: function(e, ui) { // ui.value has the number setHue(ui.value) } }); $('#hex').change(function(){ setHex( $('#hex').val() ); }); $('#set-hex').click(function(){ setHex( $('#hex').val() ); }); $("#distance-slider").slider({ min: 0, max: 1, value: 0.5, step: 0.01, slide: function(e, ui) { // ui.value has the number setDistance(ui.value) } }); scheme = new ColorScheme; setHue(0); generateColors(); $('#add-complement').click(addComplement); }); function generateColors() { $('#colors').html(''); var colors = scheme.colors(); for (var i in colors) { var c = colors[i]; var newDiv = '<div class="color" style="background-color: #' + c + '"></div>'; $('#colors').append(newDiv); } } function setHue(hue) { scheme.from_hue(hue); var bg = scheme.colors()[0]; $('#hue-box').css('background-color', '#' + bg); $('#hex').val( bg ); $('#hex-box').css('background-color', '#' + bg); generateColors(); } function setHex(hex) { // Strip possible leading hash hex = hex.replace('#', ''); $('#hex-box').css('background-color', '#' + hex); scheme.from_hex(hex); var bg = scheme.colors()[0]; $('#hue-box').css('background-color', '#' + bg); generateColors(); } function setScheme(newScheme) { if (newScheme == 'analogic') { $('#add-complement').show(); } else { $('#add-complement').hide(); } scheme.scheme(newScheme); generateColors(); } function addComplement() { if ( $('#add-complement').hasClass('active') ) { scheme.add_complement(false); } else { scheme.add_complement(true); } generateColors(); } function setDistance(distance) { scheme.distance(distance); generateColors(); } function setVariation(variation) { scheme.variation(variation); generateColors(); } function setWebSafe(websafe) { scheme.web_safe(websafe); generateColors(); } function randomHue() { var h = Math.round(Math.random() * 360); scheme.from_hue(h); generateColors(); } /* The MIT License (MIT) Copyright (c) 2014 Christian Bromann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var rgb2hex = function(color) { if(typeof color !== 'string') { // throw error of input isn't typeof string throw new Error('color has to be type of `string`'); } else if (color.substr(0, 1) === '#') { // or return if already rgb color return { hex: color, alpha: 1 }; } // parse input var digits = /(.*?)rgb(a)*\((\d+),(\d+),(\d+)(,[0-9]*\.*[0-9]+)*\)/.exec(color.replace(/\s+/g,'')); if(!digits) { // or throw error if input isn't a valid rgb(a) color throw new Error('given color (' + color + ') isn\'t a valid rgb or rgba color'); } var red = parseInt(digits[3]); var green = parseInt(digits[4]); var blue = parseInt(digits[5]); var alpha = digits[6] ? /([0-9\.]+)/.exec(digits[6])[0] : '1'; var rgb = ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1); // parse alpha value into float if(alpha.substr(0,1) === '.') { alpha = parseFloat('0' + alpha, 10); } // limit alpha value to 1 if(alpha > 1) { alpha = 1; } // cut alpha value after 2 digits after comma alpha = parseFloat(Math.round(alpha * 100), 10) / 100; return { hex: digits[1] + '#' + rgb.toString(16), alpha: alpha } }; </script> </body>