Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.exportTo('base', function() {
      8   function Color(opt_r, opt_g, opt_b, opt_a) {
      9     this.r = Math.floor(opt_r) || 0;
     10     this.g = Math.floor(opt_g) || 0;
     11     this.b = Math.floor(opt_b) || 0;
     12     this.a = opt_a;
     13   }
     14 
     15   Color.fromString = function(str) {
     16     var tmp;
     17     var values;
     18     if (str.substr(0, 4) == 'rgb(') {
     19       tmp = str.substr(4, str.length - 5);
     20       values = tmp.split(',').map(function(v) {
     21         return v.replace(/^\s+/, '', 'g');
     22       });
     23       if (values.length != 3)
     24         throw new Error('Malformatted rgb-expression');
     25       return new Color(
     26           parseInt(values[0]),
     27           parseInt(values[1]),
     28           parseInt(values[2]));
     29     } else if (str.substr(0, 5) == 'rgba(') {
     30       tmp = str.substr(5, str.length - 6);
     31       values = tmp.split(',').map(function(v) {
     32         return v.replace(/^\s+/, '', 'g');
     33       });
     34       if (values.length != 4)
     35         throw new Error('Malformatted rgb-expression');
     36       return new Color(
     37           parseInt(values[0]),
     38           parseInt(values[1]),
     39           parseInt(values[2]),
     40           parseFloat(values[3]));
     41     } else if (str[0] == '#' && str.length == 7) {
     42       return new Color(
     43           parseInt(str.substr(1, 2), 16),
     44           parseInt(str.substr(3, 2), 16),
     45           parseInt(str.substr(5, 2), 16));
     46     } else {
     47       throw new Error('Unrecognized string format.');
     48     }
     49   };
     50 
     51   Color.lerp = function(a, b, percent) {
     52     if (a.a !== undefined && b.a !== undefined)
     53       return Color.lerpRGBA(a, b, percent);
     54     return Color.lerpRGB(a, b, percent);
     55   }
     56   Color.lerpRGB = function(a, b, percent) {
     57     return new Color(
     58         ((b.r - a.r) * percent) + a.r,
     59         ((b.g - a.g) * percent) + a.g,
     60         ((b.b - a.b) * percent) + a.b);
     61   }
     62 
     63   Color.lerpRGBA = function(a, b, percent) {
     64     return new Color(
     65         ((b.r - a.r) * percent) + a.r,
     66         ((b.g - a.g) * percent) + a.g,
     67         ((b.b - a.b) * percent) + a.b,
     68         ((b.a - a.a) * percent) + a.a);
     69   }
     70 
     71   Color.prototype = {
     72     brighten: function(opt_k) {
     73       var k;
     74       k = opt_k || 0.45;
     75 
     76       return new Color(
     77           Math.min(255, this.r + Math.floor(this.r * k)),
     78           Math.min(255, this.g + Math.floor(this.g * k)),
     79           Math.min(255, this.b + Math.floor(this.b * k)));
     80     },
     81 
     82     darken: function(opt_k) {
     83       var k;
     84       k = opt_k || 0.45;
     85 
     86       return new Color(
     87           Math.min(255, this.r - Math.floor(this.r * k)),
     88           Math.min(255, this.g - Math.floor(this.g * k)),
     89           Math.min(255, this.b - Math.floor(this.b * k)));
     90     },
     91 
     92     withAlpha: function(a) {
     93       return new Color(this.r, this.g, this.b, a);
     94     },
     95 
     96     toString: function() {
     97       if (this.a !== undefined) {
     98         return 'rgba(' +
     99             this.r + ',' + this.g + ',' +
    100             this.b + ',' + this.a + ')';
    101       }
    102       return 'rgb(' + this.r + ',' + this.g + ',' + this.b + ')';
    103     }
    104   };
    105 
    106   return {
    107     Color: Color
    108   };
    109 });
    110