<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Source: dist/js/angular-colors-util.js</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <script src="scripts/jquery.min.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="styles/jaguar.css">
    
    
    <script>
    var config = {"monospaceLinks":true,"cleverLinks":true,"applicationName":"devnup/angular-colors-util - docs","default":{"outputSourceFiles":true},"linenums":true};
    </script>
    

    
</head>
<body>
<div id="wrap" class="clearfix">
    
<div class="navigation">
    <h3 class="applicationName"><a href="index.html">devnup/angular-colors-util - docs</a></h3>

    <div class="search">
        <input id="search" type="text" class="form-control input-sm" placeholder="Search Documentations">
    </div>

    <p style="color: white; margin: 14px;">Module Reference</p>

    <ul class="list">
    
        <li class="item" data-name="com.devnup.color.$color">
            <span class="title">
                <a href="com.devnup.color.$color.html">com.devnup.color.$color</a>
                
            </span>
            <ul class="members itemMembers">
            
            </ul>
            <ul class="methods itemMembers">
                
                <span class="subtitle">Methods</span>
                
                    <li data-name="com.devnup.color.$color#config"><a href="com.devnup.color.$color.html#config">config</a></li>
                
                    <li data-name="com.devnup.color.$color#config"><a href="com.devnup.color.$color.html#config">config</a></li>
                
                    <li data-name="com.devnup.color.$color#generate"><a href="com.devnup.color.$color.html#generate">generate</a></li>
                
                    <li data-name="com.devnup.color.$color#generate"><a href="com.devnup.color.$color.html#generate">generate</a></li>
                
                    <li data-name="com.devnup.color.$color#hover"><a href="com.devnup.color.$color.html#hover">hover</a></li>
                
                    <li data-name="com.devnup.color.$color#hover"><a href="com.devnup.color.$color.html#hover">hover</a></li>
                
                    <li data-name="com.devnup.color.$color~devnupColors"><a href="com.devnup.color.$color.html#~devnupColors">devnupColors</a></li>
                
                    <li data-name="com.devnup.color.$color~devnupColors"><a href="com.devnup.color.$color.html#~devnupColors">devnupColors</a></li>
                
                    <li data-name="com.devnup.color.$color~distinctColors"><a href="com.devnup.color.$color.html#~distinctColors">distinctColors</a></li>
                
                    <li data-name="com.devnup.color.$color~distinctColors"><a href="com.devnup.color.$color.html#~distinctColors">distinctColors</a></li>
                
                    <li data-name="com.devnup.color.$color~hsvToRgb"><a href="com.devnup.color.$color.html#~hsvToRgb">hsvToRgb</a></li>
                
                    <li data-name="com.devnup.color.$color~hsvToRgb"><a href="com.devnup.color.$color.html#~hsvToRgb">hsvToRgb</a></li>
                
                    <li data-name="com.devnup.color.$color~rgbToHex"><a href="com.devnup.color.$color.html#~rgbToHex">rgbToHex</a></li>
                
                    <li data-name="com.devnup.color.$color~rgbToHex"><a href="com.devnup.color.$color.html#~rgbToHex">rgbToHex</a></li>
                
                    <li data-name="com.devnup.color.$color~shadeColor"><a href="com.devnup.color.$color.html#~shadeColor">shadeColor</a></li>
                
                    <li data-name="com.devnup.color.$color~shadeColor"><a href="com.devnup.color.$color.html#~shadeColor">shadeColor</a></li>
                
                </ul>
            <ul class="typedefs itemMembers">
            
            </ul>
            <ul class="events itemMembers">
            
            </ul>
        </li>
    
        <li class="item" data-name="com.devnup.color.$color">
            <span class="title">
                <a href="com.devnup.color.$color.html">com.devnup.color.$color</a>
                
            </span>
            <ul class="members itemMembers">
            
            </ul>
            <ul class="methods itemMembers">
                
                <span class="subtitle">Methods</span>
                
                    <li data-name="com.devnup.color.$color#config"><a href="com.devnup.color.$color.html#config">config</a></li>
                
                    <li data-name="com.devnup.color.$color#config"><a href="com.devnup.color.$color.html#config">config</a></li>
                
                    <li data-name="com.devnup.color.$color#generate"><a href="com.devnup.color.$color.html#generate">generate</a></li>
                
                    <li data-name="com.devnup.color.$color#generate"><a href="com.devnup.color.$color.html#generate">generate</a></li>
                
                    <li data-name="com.devnup.color.$color#hover"><a href="com.devnup.color.$color.html#hover">hover</a></li>
                
                    <li data-name="com.devnup.color.$color#hover"><a href="com.devnup.color.$color.html#hover">hover</a></li>
                
                    <li data-name="com.devnup.color.$color~devnupColors"><a href="com.devnup.color.$color.html#~devnupColors">devnupColors</a></li>
                
                    <li data-name="com.devnup.color.$color~devnupColors"><a href="com.devnup.color.$color.html#~devnupColors">devnupColors</a></li>
                
                    <li data-name="com.devnup.color.$color~distinctColors"><a href="com.devnup.color.$color.html#~distinctColors">distinctColors</a></li>
                
                    <li data-name="com.devnup.color.$color~distinctColors"><a href="com.devnup.color.$color.html#~distinctColors">distinctColors</a></li>
                
                    <li data-name="com.devnup.color.$color~hsvToRgb"><a href="com.devnup.color.$color.html#~hsvToRgb">hsvToRgb</a></li>
                
                    <li data-name="com.devnup.color.$color~hsvToRgb"><a href="com.devnup.color.$color.html#~hsvToRgb">hsvToRgb</a></li>
                
                    <li data-name="com.devnup.color.$color~rgbToHex"><a href="com.devnup.color.$color.html#~rgbToHex">rgbToHex</a></li>
                
                    <li data-name="com.devnup.color.$color~rgbToHex"><a href="com.devnup.color.$color.html#~rgbToHex">rgbToHex</a></li>
                
                    <li data-name="com.devnup.color.$color~shadeColor"><a href="com.devnup.color.$color.html#~shadeColor">shadeColor</a></li>
                
                    <li data-name="com.devnup.color.$color~shadeColor"><a href="com.devnup.color.$color.html#~shadeColor">shadeColor</a></li>
                
                </ul>
            <ul class="typedefs itemMembers">
            
            </ul>
            <ul class="events itemMembers">
            
            </ul>
        </li>
    
    </ul>

</div>
    <div class="main">
        <h1 class="page-title" data-filename="dist_js_angular-colors-util.js.html">Source: dist/js/angular-colors-util.js</h1>
        


    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * Angular Colors Util
 * v0.0.1
 *
 * @alias com.devnup.color
 *
 * @author luis@devnup.com
 * @since 03/02/15.
 */
angular.module('com.devnup.color', [])

    .factory('$color', [

        /**
         * Angular Colors Service
         * v0.0.1
         *
         * @class
         * @alias com.devnup.color.$color
         */
            function () {

            // Default Values
            var cache = {
                value: 80,
                saturation: 70,
                number: 10
            };

            /**
             * HSV to RGB color conversion
             *
             * H runs from 0 to 360 degrees
             * S and V run from 0 to 100
             *
             * @alias com.devnup.color.$color~hsvToRgb
             *
             * @param h
             * @param s
             * @param v
             */
            var hsvToRgb = function (h, s, v) {

                var r, g, b, i, f, p, q, t;

                // Make sure our arguments stay in-range
                h = Math.max(0, Math.min(360, h));
                s = Math.max(0, Math.min(100, s));
                v = Math.max(0, Math.min(100, v));

                // We accept saturation and value arguments from 0 to 100 because that's
                // how Photoshop represents those values. Internally, however, the
                // saturation and value are calculated from a range of 0 to 1. We make
                // That conversion here.
                s /= 100;
                v /= 100;

                if (s == 0) {

                    // Achromatic (grey)
                    r = g = b = v;
                    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
                }

                h /= 60; // sector 0 to 5
                i = Math.floor(h);
                f = h - i; // factorial part of h
                p = v * (1 - s);
                q = v * (1 - s * f);
                t = v * (1 - s * (1 - f));

                switch (i) {

                    case 0:

                        r = v;
                        g = t;
                        b = p;
                        break;

                    case 1:

                        r = q;
                        g = v;
                        b = p;
                        break;

                    case 2:

                        r = p;
                        g = v;
                        b = t;
                        break;

                    case 3:

                        r = p;
                        g = q;
                        b = v;
                        break;

                    case 4:

                        r = t;
                        g = p;
                        b = v;
                        break;

                    default: // case 5:
                        r = v;
                        g = p;
                        b = q;
                }

                return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];

            };

            /**
             * RGB to HEX conversion
             *
             * @alias com.devnup.color.$color~rgbToHex
             *
             * @param red
             * @param green
             * @param blue
             * @returns {string}
             */
            var rgbToHex = function (red, green, blue) {
                var decColor = red + 256 * green + 256 * 256 * blue;
                return decColor.toString(16);
            };

            /**
             * Shade a hex color
             *
             * @alias com.devnup.color.$color~shadeColor
             *
             * @param color
             * @param percent
             * @returns {string}
             */
            var shadeColor = function (color, percent) {
                var num = parseInt(color, 16),
                    amt = Math.round(2.55 * percent),
                    R = (num >> 16) + amt,
                    G = (num >> 8 &amp; 0x00FF) + amt,
                    B = (num &amp; 0x0000FF) + amt;
                return (0x1000000 + (R &lt; 255 ? R &lt; 1 ? 0 : R : 255) * 0x10000 + (G &lt; 255 ? G &lt; 1 ? 0 : G : 255) * 0x100 + (B &lt; 255 ? B &lt; 1 ? 0 : B : 255)).toString(16).slice(1);
            };

            /**
             * Generate distinct colors based on HSV
             *
             * @alias com.devnup.color.$color~distinctColors
             *
             * @param total
             * @returns {Array}
             */
            var distinctColors = function (total) {
                var i = 360 / (total); // distribute the colors evenly on the hue range
                var r = []; // hold the generated colors
                for (var x = 0; x &lt; total; x++) {
                    r.push(hsvToRgb(i * x, cache.saturation, cache.value)); // changes on saturation and value changes the final result
                }
                return r;
            };

            /**
             * Generate distinct colors based on Devnup branding
             *
             * @alias com.devnup.color.$color~devnupColors
             *
             * @param n
             * @returns {Array}
             */
            var devnupColors = function (n) {
                var colorsHex = [];
                colorsDec = distinctColors(n);
                for (k in colorsDec) {
                    colorsHex[k] = "#" + rgbToHex(colorsDec[k][0], colorsDec[k][1], colorsDec[k][2]);
                }
                return colorsHex;
            };

            return {

                /**
                 * Set generator configuration values.
                 *
                 * @alias com.devnup.color.$color#config
                 *
                 * @param {Object} cfg The input configuration
                 * @param {Number} [cfg.value] The color value
                 * @param {Number} [cfg.saturation] The color saturation
                 * @param {Number} [cfg.number] The color number
                 */
                config: function (cfg) {
                    cache.value = cfg.value || cache.value;
                    cache.saturation = cfg.saturation || cache.saturation;
                    cache.number = cfg.number || cache.number;
                },

                /**
                 * Generate a new random color set.
                 *
                 * @alias com.devnup.color.$color#generate
                 *
                 * @param {Number} number The color count
                 * @param {Boolean} [branding] If true generate using branding colors as reference
                 */
                generate: function (number, branding) {

                    // Generate colors based on branding
                    if (number &amp;&amp; number > 0 &amp;&amp; branding) {
                        return devnupColors(number);
                    }

                    // Generate distinct colors
                    else {
                        return distinctColors(number);
                    }

                },

                /**
                 * Hover a hex color.
                 *
                 * @alias com.devnup.color.$color#hover
                 *
                 * @param {Number} color The color to hover.
                 * @param {Number} [percent] The value to hover, default: 20%
                 */
                hover: function (color, percent) {
                    percent = percent || 20;
                    return shadeColor(color, percent);
                }

            }

        }]);</code></pre>
        </article>
    </section>






        

        <footer>
            Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta1</a> on Wed Mar 04 2015 23:51:49 GMT-0300 (BRT)
        </footer>
    </div>
</div>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
<script src="scripts/main.js"></script>
</body>
</html>
