[Pkg-javascript-commits] [jquery-minicolors] 19/46: Always output correct format

David Prévot taffit at moszumanska.debian.org
Sun Oct 25 17:18:25 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository jquery-minicolors.

commit 9aa053e35055f4824b252e9499dd63046b3ebc67
Author: Cory LaViska <cory at abeautifulsite.net>
Date:   Thu Sep 17 16:02:49 2015 -0400

    Always output correct format
---
 jquery.minicolors.js | 56 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 12 deletions(-)

diff --git a/jquery.minicolors.js b/jquery.minicolors.js
index 1870503..5fc9453 100644
--- a/jquery.minicolors.js
+++ b/jquery.minicolors.js
@@ -737,20 +737,35 @@
 
     // Parses a string and returns a valid RGB(A) string when possible
     function parseRgb(string, obj) {
-        values = string.replace(/[^\d,.]/g, '');
-        rgba = values.split(',');
+
+        var values = string.replace(/[^\d,.]/g, ''),
+            rgba = values.split(','),
+            output;
+
+        rgba[0] = keepWithin(parseInt(rgba[0], 10), 0, 255);
+        rgba[1] = keepWithin(parseInt(rgba[1], 10), 0, 255);
+        rgba[2] = keepWithin(parseInt(rgba[2], 10), 0, 255);
+        if( rgba[3] ) {
+            rgba[3] = keepWithin(parseFloat(rgba[3], 10), 0, 1);
+        }
+
+        // Return RGBA object
+        if( obj ) {
+            return {
+                r: rgba[0],
+                g: rgba[1],
+                b: rgba[2],
+                a: rgba[3] ? rgba[3] : null
+            };
+        }
+
+        // Return RGBA string
         if( rgba[3] ) {
-            output = 'rgba(' + keepWithin(rgba[0], 0, 255) +
-                ', ' + keepWithin(rgba[1], 0, 255) +
-                ', ' + keepWithin(rgba[2], 0, 255) +
-                ', ' + keepWithin(rgba[3], 0, 1) + ')';
+            return 'rgba(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ', ' + rgba[3] + ')';
         } else {
-            output = 'rgb(' + keepWithin(rgba[0], 0, 255) +
-                ', ' + keepWithin(rgba[1], 0, 255) +
-                ', ' + keepWithin(rgba[2], 0, 255) + ')';
+            return 'rgb(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ')';
         }
 
-        return (isRgb(string)) ? output : false;
     }
 
     // Parses a string and returns a valid color string when possible
@@ -924,7 +939,8 @@
         .on('blur.minicolors', '.minicolors-input', function() {
             var input = $(this),
                 keywords = input.attr('data-keywords'),
-                settings = input.data('minicolors-settings');
+                settings = input.data('minicolors-settings'),
+                rgba;
             if( !input.data('minicolors-initialized') ) return;
 
             // Get array of lowercase keywords
@@ -936,7 +952,23 @@
             if( input.val() !== '' && $.inArray(input.val().toLowerCase(), keywords) > -1 ) {
                 value = convertCase(input.val());
             } else {
-                value = isRgb(input.val()) ? parseRgb(input.val()) : parseHex(input.val(), true);
+
+                // Get RGBA values for easy conversion
+                if( isRgb(input.val()) ) {
+                    rgba = parseRgb(input.val(), true);
+                } else {
+                    rgba = hex2rgb(parseHex(input.val(), true));
+                }
+
+                // Convert to format
+                if( settings.format === 'rgb' ) {
+                    value = settings.opacity ?
+                        parseRgb('rgba(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ',' + input.attr('data-opacity') + ')') :
+                        parseRgb('rgb(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ')');
+                } else {
+                    value = rgb2hex(rgba);
+                }
+
             }
 
             // Set input value

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/jquery-minicolors.git



More information about the Pkg-javascript-commits mailing list