SOURCE CODE: Color Sort by RGB Proximity
VIEW EXAMPLE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Color Sort by RGB Proximity | JavaScript Examples | UIZE JavaScript Framework</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="keywords" content="featured color Uize.Color.xUtil"/>
  <meta name="description" content="When you have a limited palette of colors, how can you sort the colors to find the closest matches to a desired color? This example shows you how."/>
  <link rel="alternate" type="application/rss+xml" title="UIZE JavaScript Framework - Latest News" href="http://www.uize.com/latest-news.rss"/>
  <link rel="stylesheet" href="../css/page.css"/>
  <link rel="stylesheet" href="../css/page.example.css"/>
  <style tyle="text/css">
    .colorSwatch {
      width:1px;
      height:340px;
      float:left;
      cursor:pointer;
    }
  </style>
</head>

<body>

<script type="text/javascript" src="../js/Uize.js"></script>

<h1 class="header">
  <a id="page-homeLink" href="../index.html" title="UIZE JavaScript Framework home"></a>
  <a href="../index.html" class="homeLinkText" title="UIZE JavaScript Framework home">UIZE JavaScript Framework</a>
</h1>

<div class="main">
  <h1 class="document-title">
    <a href="../javascript-examples.html" class="breadcrumb breadcrumbWithArrow">JAVASCRIPT EXAMPLES</a>
    Color Sort by RGB Proximity
    <div class="pageActionsShell">
      <div id="page-actions" class="pageActions"><a href="source-code/color-sort-by-rgb-proximity.html" class="buttonLink">SOURCE</a></div>
    </div>
  </h1>

  <!-- explanation copy -->

  <div class="explanation">
    <p>This example demonstrates the proximity-based color sorting functionality of the UIZE JavaScript Framework, as implemented in the <a href="../reference/Uize.Color.xUtil.html"><code>Uize.Color.xUtil</code></a> module. On the right of the interface is a large rectangle, with a sorted array of colors. On the left of the interface is an RGB color picker, with sliders for selecting levels for the red, green, and blue color channels of an RGB color. As you use the sliders to change your desired color, the finite set of available colors is re-sorted so that the colors most like your desired color are rearranged to be on the left. Additionally, these better matching colors are displayed using wider bars so you can inspect them more closely to make a better choice between the candidates. Color sorting functionality is useful when assisting a user in finding a color from a fixed color palette that is closest to their preferred color.</p>
  </div>

  <!-- "wireframe" for the UI -->

  <table border="0" cellspacing="0" cellpadding="0" style="margin:auto;">
    <tr>
      <td class="heading">Choose Color</td>
      <td style="width:20px;"></td>
      <td class="heading">Your Desired Color</td>
      <td style="width:20px;"></td>
      <td class="heading">Closest Matches From Available Colors (ie. limited color palette)</td>
    </tr>
    <tr>
      <td id="page_desiredColor"></td>
      <td></td>
      <td id="page-desiredColor" style="width:140px;"></td>
      <td></td>
      <td id="page-colorSwatches" style="width:442px;"></td>
    </tr>
  </table>
</div>

<!-- JavaScript code to generate the color swatches -->

<script type="text/javascript">

Uize.require (
  [
    'UizeSite.Page.Example.library',
    'UizeSite.Page.Example',
    'Uize.Color',
    'Uize.Color.xUtil',
    'Uize.Widgets.RgbSliders.Widget',
    'Uize.Widgets.ColorInfo.Widget',
    'Uize.Dom.Basics',
    'Uize.Dom.Event',
    'Uize.Tooltip',
    'Uize.Fade.xFactory',
    'Uize.Curve'
  ],
  function () {
    'use strict';

    /*** create the example page widget ***/
      var page = window.page = UizeSite.Page.Example ();

    /*** sort colors and update swatches ***/
      function sortColorSwatches () {
        var desiredColorValue = page.children.desiredColor.get ('value');
        page.setNodeStyle ('desiredColor',{backgroundColor:'#' + desiredColorValue});
        Uize.Color.sort (colors,desiredColorValue);
        for (var colorNo = -1; ++colorNo < totalColors;)
          page.setNodeStyle ('colorSwatch' + colorNo,{backgroundColor:colors [colorNo].to ()})
        ;
      }

    /*** randomize colors ***/
      var
        totalColors = 200,
        colors = []
      ;
      function randomizeColors () {
        for (var colorNo = -1; ++colorNo < totalColors;)
          (colors [colorNo] || (colors [colorNo] = Uize.Color ()).setEncoding ('RGB string')).random ()
        ;
        sortColorSwatches ();
      }

    /*** build the swatches HTML ***/
      var
        swatchesHtmlChunks = [],
        swatchNodeName,
        swatchNodeNames = []
      ;
      for (var colorNo = -1; ++colorNo < totalColors;) {
        swatchNodeNames.push (swatchNodeName = 'colorSwatch' + colorNo);
        swatchesHtmlChunks.push (
          '<div id="page-' + swatchNodeName + '" class="colorSwatch" style="width:' + (colorNo < 8 ? 16 : colorNo < 16 ? 6 : colorNo < 32 ? 4 : colorNo < 64 ? 2 : 1) + 'px;"></div>'
        );
      }

    /*** add the RGB reference color picker ***/
      page.addChild ('desiredColor',Uize.Widgets.RgbSliders.Widget,{value:'00aaff',sliderHeight:260,built:false})
        .wire ('Changed.value',sortColorSwatches);

    /*** add the color info widget ***/
      var colorInfo = page.addChild ('colorInfo',Uize.Widgets.ColorInfo.Widget,{built:false});

    /*** wire up the page widget ***/
      page.wireUi ();

    /*** initialization ***/
      colorInfo.setNodeStyle ('',{display:'none'});
      page.setNodeInnerHtml ('colorSwatches',swatchesHtmlChunks.join (''));

      /*** wire up the swatch mouseover and click behaviors ***/
        function updateColorInfo () {colorInfo.set ({value:this.style.backgroundColor})}
        page.wireNode (
          'colorSwatches',
          {
            mouseover:function () {Uize.Tooltip.showTooltip (colorInfo.getNode ())},
            mouseout:function () {Uize.Tooltip.showTooltip (colorInfo.getNode (),false)},
            click:function (event) {
              var target = Uize.Dom.Event.target (event);
              Uize.Fade.fade (
                function (value) {page.children.desiredColor.set ({value:Uize.Color.to (value,'hex')})},
                [Uize.Color.to (page.children.desiredColor + '','RGB array')],
                [Uize.Color.to (Uize.Dom.Basics.getStyle (target,'backgroundColor'),'RGB array')],
                1500,
                {curve:Uize.Curve.easeInOutPow (4)}
              );
              updateColorInfo.apply (target);
            }
          }
        );
        page.wireNode (swatchNodeNames,'mouseover',updateColorInfo);

      randomizeColors ();
  }
);

</script>

</body>
</html>