Moved color conversion methods into `ColorUtil`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 22 Mar 2017 13:12:02 +0000 (14:12 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 22 Mar 2017 13:12:09 +0000 (14:12 +0100)
See #2238

wcfsetup/install/files/js/WCF.ColorPicker.js
wcfsetup/install/files/js/WoltLabSuite/Core/ColorUtil.js
wcfsetup/install/files/js/require.config.js

index ae27a45dca9cf8b69cadb96e1df56ce080d0c658..eabc6d45facf0e4407fd78208005c4a8c1734714 100644 (file)
@@ -520,68 +520,7 @@ WCF.ColorPicker = Class.extend({
         * @return      object
         */
        hsvToRgb: function(h, s, v) {
-               var $rgb = { r: 0, g: 0, b: 0 };
-               var $h, $f, $p, $q, $t;
-               
-               $h = Math.floor(h / 60);
-               $f = h / 60 - $h;
-               
-               s /= 100;
-               v /= 100;
-               
-               $p = v * (1 - s);
-               $q = v * (1 - s * $f);
-               $t = v * (1 - s * (1 - $f));
-               
-               if (s == 0) {
-                       $rgb.r = $rgb.g = $rgb.b = v;
-               }
-               else {
-                       switch ($h) {
-                               case 1:
-                                       $rgb.r = $q;
-                                       $rgb.g = v;
-                                       $rgb.b = $p;
-                               break;
-                               
-                               case 2:
-                                       $rgb.r = $p;
-                                       $rgb.g = v;
-                                       $rgb.b = $t;
-                               break;
-                               
-                               case 3:
-                                       $rgb.r = $p;
-                                       $rgb.g = $q;
-                                       $rgb.b = v;
-                               break;
-                               
-                               case 4:
-                                       $rgb.r = $t;
-                                       $rgb.g = $p;
-                                       $rgb.b = v;
-                               break;
-                               
-                               case 5:
-                                       $rgb.r = v;
-                                       $rgb.g = $p;
-                                       $rgb.b = $q;
-                               break;
-                               
-                               case 0:
-                               case 6:
-                                       $rgb.r = v;
-                                       $rgb.g = $t;
-                                       $rgb.b = $p;
-                               break;
-                       }
-               }
-               
-               return {
-                       r: Math.round($rgb.r * 255),
-                       g: Math.round($rgb.g * 255),
-                       b: Math.round($rgb.b * 255)
-               };
+               return window.__wcf_bc_colorUtil.hsvToRgb(h, s, v);
        },
        
        /**
@@ -595,52 +534,7 @@ WCF.ColorPicker = Class.extend({
         * @return      object
         */
        rgbToHsv: function(r, g, b) {
-               var $h, $s, $v;
-               var $max, $min, $diff;
-               
-               r /= 255;
-               g /= 255;
-               b /= 255;
-               
-               $max = Math.max(Math.max(r, g), b);
-               $min = Math.min(Math.min(r, g), b);
-               $diff = $max - $min;
-               
-               $h = 0;
-               if ($max !== $min) {
-                       switch ($max) {
-                               case r:
-                                       $h = 60 * (0 + (g - b) / $diff);
-                               break;
-                               
-                               case g:
-                                       $h = 60 * (2 + (b - r) / $diff);
-                               break;
-                               
-                               case b:
-                                       $h = 60 * (4 + (r - g) / $diff);
-                               break;
-                       }
-                       
-                       if ($h < 0) {
-                               $h += 360;
-                       }
-               }
-               
-               if ($max === 0) {
-                       $s = 0;
-               }
-               else {
-                       $s = $diff / $max;
-               }
-               
-               $v = $max;
-               
-               return {
-                       h: Math.round($h),
-                       s: Math.round($s * 100),
-                       v: Math.round($v * 100)
-               };
+               return window.__wcf_bc_colorUtil.rgbToHsv(r, g, b);
        },
        
        /**
@@ -650,33 +544,7 @@ WCF.ColorPicker = Class.extend({
         * @return      object
         */
        hexToRgb: function(hex) {
-               if (/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(hex)) {
-                       // only convert #abc and #abcdef
-                       hex = hex.split('');
-                       
-                       // drop the hashtag
-                       if (hex[0] === '#') {
-                               hex.shift();
-                       }
-                       
-                       // parse shorthand #xyz
-                       if (hex.length === 3) {
-                               return {
-                                       r: parseInt(hex[0] + '' + hex[0], 16),
-                                       g: parseInt(hex[1] + '' + hex[1], 16),
-                                       b: parseInt(hex[2] + '' + hex[2], 16)
-                               };
-                       }
-                       else {
-                               return {
-                                       r: parseInt(hex[0] + '' + hex[1], 16),
-                                       g: parseInt(hex[2] + '' + hex[3], 16),
-                                       b: parseInt(hex[4] + '' + hex[5], 16)
-                               };
-                       }
-               }
-               
-               return Number.NaN;
+               return window.__wcf_bc_colorUtil.hexToRgb(hex);
        },
        
        /**
@@ -690,6 +558,6 @@ WCF.ColorPicker = Class.extend({
         * @return      string
         */
        rgbToHex: function(r, g, b) {
-               return ("0123456789ABCDEF".charAt((r - r % 16) / 16) + '' + "0123456789ABCDEF".charAt(r % 16)) + '' + ("0123456789ABCDEF".charAt((g - g % 16) / 16) + '' + "0123456789ABCDEF".charAt(g % 16)) + '' + ("0123456789ABCDEF".charAt((b - b % 16) / 16) + '' + "0123456789ABCDEF".charAt(b % 16));
+               return window.__wcf_bc_colorUtil.rgbToHex(r, g, b);
        }
 });
\ No newline at end of file
index a6d5f8f1966564518fb29ac0099e5a18e6f7ccc8..93f62237062b76dc20dc719d790f2c33f8f66af3 100644 (file)
-define([], function() {
+define([], function () {
        "use strict";
        
        var ColorUtil = {
+               /**
+                * Converts a HSV color into RGB.
+                *
+                * @see https://secure.wikimedia.org/wikipedia/de/wiki/HSV-Farbraum#Transformation_von_RGB_und_HSV
+                *
+                * @param       {int}           h
+                * @param       {int}           s
+                * @param       {int}           v
+                * @return      {Object}
+                */
+               hsvToRgb: function(h, s, v) {
+                       var rgb = { r: 0, g: 0, b: 0 };
+                       var h2, f, p, q, t;
+                       
+                       h2 = Math.floor(h / 60);
+                       f = h / 60 - h2;
+                       
+                       s /= 100;
+                       v /= 100;
+                       
+                       p = v * (1 - s);
+                       q = v * (1 - s * f);
+                       t = v * (1 - s * (1 - f));
+                       
+                       if (s == 0) {
+                               rgb.r = rgb.g = rgb.b = v;
+                       }
+                       else {
+                               switch (h2) {
+                                       case 1:
+                                               rgb.r = q;
+                                               rgb.g = v;
+                                               rgb.b = p;
+                                               break;
+                                       
+                                       case 2:
+                                               rgb.r = p;
+                                               rgb.g = v;
+                                               rgb.b = t;
+                                               break;
+                                       
+                                       case 3:
+                                               rgb.r = p;
+                                               rgb.g = q;
+                                               rgb.b = v;
+                                               break;
+                                       
+                                       case 4:
+                                               rgb.r = t;
+                                               rgb.g = p;
+                                               rgb.b = v;
+                                               break;
+                                       
+                                       case 5:
+                                               rgb.r = v;
+                                               rgb.g = p;
+                                               rgb.b = q;
+                                               break;
+                                       
+                                       case 0:
+                                       case 6:
+                                               rgb.r = v;
+                                               rgb.g = t;
+                                               rgb.b = p;
+                                               break;
+                               }
+                       }
+                       
+                       return {
+                               r: Math.round(rgb.r * 255),
+                               g: Math.round(rgb.g * 255),
+                               b: Math.round(rgb.b * 255)
+                       };
+               },
+               
+               /**
+                * Converts a RGB color into HSV.
+                *
+                * @see https://secure.wikimedia.org/wikipedia/de/wiki/HSV-Farbraum#Transformation_von_RGB_und_HSV
+                *
+                * @param       {int}           r
+                * @param       {int}           g
+                * @param       {int}           b
+                * @return      {Object}
+                */
+               rgbToHsv: function(r, g, b) {
+                       var h, s, v;
+                       var max, min, diff;
+                       
+                       r /= 255;
+                       g /= 255;
+                       b /= 255;
+                       
+                       max = Math.max(Math.max(r, g), b);
+                       min = Math.min(Math.min(r, g), b);
+                       diff = max - min;
+                       
+                       h = 0;
+                       if (max !== min) {
+                               switch (max) {
+                                       case r:
+                                               h = 60 * ((g - b) / diff);
+                                               break;
+                                       
+                                       case g:
+                                               h = 60 * (2 + (b - r) / diff);
+                                               break;
+                                       
+                                       case b:
+                                               h = 60 * (4 + (r - g) / diff);
+                                               break;
+                               }
+                               
+                               if (h < 0) {
+                                       h += 360;
+                               }
+                       }
+                       
+                       if (max === 0) {
+                               s = 0;
+                       }
+                       else {
+                               s = diff / max;
+                       }
+                       
+                       v = max;
+                       
+                       return {
+                               h: Math.round(h),
+                               s: Math.round(s * 100),
+                               v: Math.round(v * 100)
+                       };
+               },
+               
                /**
                 * Converts HEX into RGB.
                 *
-                * @param       string          hex     hex value as #ccc or #abc123
-                * @return      object          r-g-b values
+                * @param       {string}        hex
+                * @return      {Object}
                 */
                hexToRgb: function(hex) {
-                       hex = hex.replace(/^#/, '');
-                       if (/^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(hex)) {
-                               // only convert abc and abcdef
-                               hex = hex.split('');
+                       if (/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(hex)) {
+                               // only convert #abc and #abcdef
+                               var parts = hex.split('');
+                               
+                               // drop the hashtag
+                               if (parts[0] === '#') {
+                                       parts.shift();
+                               }
                                
                                // parse shorthand #xyz
-                               if (hex.length === 3) {
+                               if (parts.length === 3) {
                                        return {
-                                               r: parseInt(hex[0] + '' + hex[0], 16),
-                                               g: parseInt(hex[1] + '' + hex[1], 16),
-                                               b: parseInt(hex[2] + '' + hex[2], 16)
+                                               r: parseInt(parts[0] + '' + parts[0], 16),
+                                               g: parseInt(parts[1] + '' + parts[1], 16),
+                                               b: parseInt(parts[2] + '' + parts[2], 16)
                                        };
                                }
                                else {
                                        return {
-                                               r: parseInt(hex[0] + '' + hex[1], 16),
-                                               g: parseInt(hex[2] + '' + hex[3], 16),
-                                               b: parseInt(hex[4] + '' + hex[5], 16)
+                                               r: parseInt(parts[0] + '' + parts[1], 16),
+                                               g: parseInt(parts[2] + '' + parts[3], 16),
+                                               b: parseInt(parts[4] + '' + parts[5], 16)
                                        };
                                }
                        }
@@ -35,20 +173,20 @@ define([], function() {
                },
                
                /**
-                * Converts RGB into HEX.
+                * Converts RGB into HEX.
                 *
                 * @see http://www.linuxtopia.org/online_books/javascript_guides/javascript_faq/rgbtohex.htm
-                * 
-                * @param       {(int|string)}  r       red or rgb(1, 2, 3) or rgba(1, 2, 3, .4)
-                * @param       {int}           g       green
-                * @param       {int}           b       blue
-                * @return      {string}        hex value #abc123
+                *
+                * @param       {int}           r
+                * @param       {int}           g
+                * @param       {int}           b
+                * @return      {string}
                 */
                rgbToHex: function(r, g, b) {
                        var charList = "0123456789ABCDEF";
                        
                        if (g === undefined) {
-                               if (r.match(/^rgba?\((\d+), ?(\d+), ?(d\+)(?:, ?[0-9.]+)?\)$/)) {
+                               if (r.toString().match(/^rgba?\((\d+), ?(\d+), ?(d\+)(?:, ?[0-9.]+)?\)$/)) {
                                        r = RegExp.$1;
                                        g = RegExp.$2;
                                        b = RegExp.$3;
@@ -59,5 +197,8 @@ define([], function() {
                }
        };
        
+       // WCF.ColorPicker compatibility (color format conversion)
+       window.__wcf_bc_colorUtil = ColorUtil;
+       
        return ColorUtil;
-});
\ No newline at end of file
+});
index d8f3a3eebf4adcd5e6de80e2f37b2941d552e31e..4beb37ecefb5e8480f07ee45e82348576a1f750e 100644 (file)
@@ -16,6 +16,7 @@ requirejs.config({
                        'AjaxJsonp': 'WoltLabSuite/Core/Ajax/Jsonp',
                        'AjaxRequest': 'WoltLabSuite/Core/Ajax/Request',
                        'CallbackList': 'WoltLabSuite/Core/CallbackList',
+                       'ColorUtil': 'WoltLabSuite/Core/ColorUtil',
                        'Core': 'WoltLabSuite/Core/Core',
                        'DateUtil': 'WoltLabSuite/Core/Date/Util',
                        'Dictionary': 'WoltLabSuite/Core/Dictionary',