Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(colors): directive to use any color from any palette everywhere
Browse files Browse the repository at this point in the history
- added directive

fixes #1269
  • Loading branch information
EladBezalel committed Mar 29, 2016
1 parent 0b89a87 commit 4b3274a
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/components/colors/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @ngdoc module
* @name material.components.colors
*
* @description
* Colors directive
*/
angular.module('material.components.colors', [
'material.core'
])
.directive('mdColors', mdColorsDirective);

/**
* @ngdoc directive
* @name mdColors
* @module material.components.colors
*
* @restrict A
*
* @description
*
*
* @usage
* <hljs lang="html">
* </hljs>
*
*/

function mdColorsDirective($mdColorPalette, $mdTheming) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
scope.$watch(attrs.mdColors, function (val) {
Object.keys(val).forEach(function (key) {
var split = val[key].split('-');

var theme = 'default';

// Checking if the first section is actually a theme
if ($mdTheming.THEMES[split[0]]) {
theme = split.splice(0, 1);
}

var palette = split[0];

// If the next section is one of the palettes we assume it's a two word palette
if (Object.keys($mdColorPalette).indexOf(split[1]) !== -1) {
palette += '-' + split.splice(1, 1);
}
else {
// Two word palette can be also written in camelCase, forming camelCase to dash-case
palette = palette.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}

// If the palette is not in the palette list it's one of primary/accent/warn/background
if (Object.keys($mdColorPalette).indexOf(palette) === -1) {
var scheme = $mdTheming.THEMES[theme].colors[palette];

if (!scheme) {
return console.error('mdColors: couldn\'t find \'' + palette + '\' in the palettes.');
}

palette = scheme.name;
}

var hue = split[1] || 500;
var opacity = split[2] || 1;

var color = $mdColorPalette[palette][hue].value;

elem.css(key, 'rgba(' + color[0] + ', ' + color[1] + ', ' + color[2] + ', ' + opacity + ')');
});
})
}
}
}
29 changes: 29 additions & 0 deletions src/components/colors/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div ng-controller="BasicDemoCtrl as ctrl" layout="column" ng-cloak>
<md-card md-colors="{background: 'myTheme-accent-500-0.87'}">
<md-card-title>
<span md-colors="{color: 'amber-A700'}">Now you can use</span>
</md-card-title>
</md-card>
<md-card md-colors="{background: 'primary'}">
<md-card-title>
<span md-colors="{color: 'accent-A200'}">Any color from any palette</span>
</md-card-title>
</md-card>

<md-card>
<md-card-title>
<span md-colors="{color: 'red'}">E</span>
<span md-colors="{color: 'pink'}">V</span>
<span md-colors="{color: 'purple'}">E</span>
<span md-colors="{color: 'deepPurple'}">R</span>
<span md-colors="{color: 'indigo'}">Y</span>
<span md-colors="{color: 'blue'}">W</span>
<span md-colors="{color: 'light-blue'}">H</span>
<span md-colors="{color: 'cyan'}">E</span>
<span md-colors="{color: 'teal'}">R</span>
<span md-colors="{color: 'green'}">E</span>
<span md-colors="{color: 'lightGreen'}">!</span>
<span md-colors="{color: 'lime'}">!</span>
</md-card-title>
</md-card>
</div>
19 changes: 19 additions & 0 deletions src/components/colors/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function () {
'use strict';
angular
.module('colorsDemo', ['ngMaterial', 'ngMessages'])
.config(function ($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('purple')
.accentPalette('light-green');

$mdThemingProvider.theme('myTheme')
.primaryPalette('indigo')
.accentPalette('pink');
})
.controller('BasicDemoCtrl', DemoCtrl);

function DemoCtrl () {

}
})();

0 comments on commit 4b3274a

Please sign in to comment.