Skip to content

Commit

Permalink
Updating to new Material Design picker spec
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Mulder committed Dec 7, 2015
1 parent baf84d3 commit 7d383d6
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 24 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"paper-slider": "PolymerElements/paper-slider#~1.0.8",
"neon-animation": "PolymerElements/neon-animation#~1.0.8",
"paper-styles": "PolymerElements/paper-styles#~1.0.13",
"whenever.js": "David-Mulder/whenever.js#~0.0.1"
"whenever.js": "David-Mulder/whenever.js#~0.0.1",
"iron-form-element-behavior": "PolymerElements/iron-form-element-behavior#~1.0.6"
}
}
2 changes: 1 addition & 1 deletion color-names.json

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions demo/demo.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!doctype html>
<html>
<head>
Expand All @@ -19,9 +10,11 @@

<link href="../../paper-dialog/paper-dialog.html" rel="import">
<link href="../../paper-button/paper-button.html" rel="import">
<link href="../../paper-input/paper-input.html" rel="import">

<link href="../paper-color-circle.html" rel="import">
<link href="../paper-color-picker.html" rel="import">
<link href="../paper-color-input.html" rel="import">

<style shim-shadowdom>

Expand Down Expand Up @@ -65,7 +58,18 @@ <h1>Standalone</h1>
<paper-color-circle style="margin:20px;" type="hsl" shape="huebox"></paper-color-circle>

</section>


<section>
<h1><code>&lt;paper-color-input&gt;</code></h1>
<div style="width: 300px;">
<paper-color-input></paper-color-input>
<paper-color-input value='{"red":255,"green":155,"blue":55}'></paper-color-input>
<paper-color-input value='{"red":255,"green":155,"blue":55}' shape="huebox" label="Color with hue picker"></paper-color-input>
<paper-color-input value='{"red":0,"green":255,"blue":0}' shape="huebox" label="Color with hue picker"></paper-color-input>
<paper-color-input value='{"red":12,"green":34,"blue":56}' shape="square" label="Color with square picker"></paper-color-input>
</div>
</section>

<section>
<h1>Dialog</h1>

Expand Down
67 changes: 67 additions & 0 deletions demo/no-label-loading-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html>
<head>

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">

<title>paper-input</title>

<script src="../../webcomponentsjs/webcomponents.js"></script>

<link href="../../paper-dialog/paper-dialog.html" rel="import">
<link href="../../paper-button/paper-button.html" rel="import">
<link href="../../paper-input/paper-input.html" rel="import">

<link href="../paper-color-circle.html" rel="import">
<link href="../paper-color-picker.html" rel="import">
<link href="../paper-color-input.html" rel="import">

<style shim-shadowdom>

body {
font-family: RobotoDraft, 'Helvetica Neue', Helvetica, Arial;
font-size: 14px;
margin: 0;
padding: 24px;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-touch-callout: none;
}
paper-color-circle{
width:200px;
height:200px;
}
section{
padding-bottom:30px;
}
.buttons{
padding-left:20px;
}
.buttons > div{
float:left;
}
</style>

</head>
<body unresolved>

<section>
<h1>Dialog</h1>

<br>

<div class="buttons">

<div>
<paper-button raised onclick="this.parentElement.querySelector('paper-color-picker').open()">
Standard dialog
</paper-button>

<paper-color-picker no-full-names></paper-color-picker>
</div>

</div>

</section>

</body>
</html>
66 changes: 66 additions & 0 deletions paper-color-circle.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,53 @@

return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
function rgb2hsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;

if(max == min){
h = s = 0;
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}

return [h, s, l];
}
var rgb2hsv = function(r,g,b) {
var b, delta, g, h, max, min, r, s, v;
min = Math.min(r, g, b);
max = Math.max(r, g, b);
delta = max - min;
v = max / 255.0;
if (max === 0) {
h = Number.NaN;
s = 0;
} else {
s = delta / max;
if (r === max) {
h = (g - b) / delta;
}
if (g === max) {
h = 2 + (b - r) / delta;
}
if (b === max) {
h = 4 + (r - g) / delta;
}
h *= 60;
if (h < 0) {
h += 360;
}
}
return [h/360, s, v];
};

Polymer({
is: 'paper-color-circle',
Expand Down Expand Up @@ -141,11 +188,21 @@
return hsl2rgb;
}
},
rgb2hsl: {
value: function(){
return rgb2hsl;
}
},
hsv2rgb: {
value: function(){
return hsv2rgb;
}
},
rgb2hsv: {
value: function(){
return rgb2hsv;
}
},
/**
* In case of a huebox, the hue from 0 to 1
*
Expand Down Expand Up @@ -375,6 +432,15 @@
this.redraw();
},
colorChanged: function(){
if(this.type == 'hsl'){
var hsl = this.rgb2hsl(this.color.red, this.color.green, this.color.blue);
this.hue = hsl[0];
this.lightness = hsl[2];
}else if(this.type == 'hsv'){
var hsv= this.rgb2hsv(this.color.red, this.color.green, this.color.blue);
this.hue = hsv[0];
this.value = hsv[2];
}
if(this._domReady)
this.redraw();
},
Expand Down
113 changes: 113 additions & 0 deletions paper-color-input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<link rel="import" href="./paper-color-picker.html">

<!--
@demo demo/index.html
@hero hero.svg
-->

<dom-module id="paper-color-input">
<template>
<style>
:host {
}
#colorPreview{
height: 16px;
width: 16px;
border: 1px solid rgba(0,0,0,.1);
border-radius: 1px;
margin: 2px;
}
#noColor{
width: 22.6px;
height: 1px;
border-top: 1px solid rgba(0,0,0,.1);
transform-origin: 0px 0px;
transform: translateY(15.5px) rotate(-45deg);
}
</style>
<paper-input-container id="input" always-float-label="{{_isValueDefined(value.*)}}">
<label>{{label}}</label>
<div class="paper-input-input">{{colorName}}&nbsp;</div>
<div id="colorPreview" suffix style$="{{_computeBackgroundColor(value.*)}}">
<div id="noColor" hidden="{{_isValueDefined(value.*)}}"></div>
</div>
</paper-input-container>

<paper-color-picker id="picker" color="{{value}}" color-as-string="{{colorName}}" shape="{{shape}}" type="{{type}}"></paper-color-picker>

</template>

<script>
Polymer({
is: 'paper-color-input',
properties: {
value: {
type: Object,
notify: true
},
label: {
type: String,
value: 'Color'
},
colorName: String,

/**
* *square*, *circle* or *huebox*
*
* @attribute shape
* @type string
* @default 'circle'
*/
shape: {
type: String,
value: 'circle',
notify: true
},

/**
* *hsv* or *hsl*
*
* @attribute type
* @type string
* @default 'hsv'
*/
type: {
type: String,
value: 'hsv',
notify: true
},
},
behaviors: [
/* Polymer.IronFormElementBehavior */ // disabled till https://github.com/Polymer/polymer/issues/3167 gets fixed
],
listeners: {
'input.tap': 'openColorPicker'
},
// Element Lifecycle
ready: function() {
if(this._isValueDefined()){
this.$.picker.set('immediateColor', this.value);
var listenOnce;
this.$.picker.addEventListener('immediate-color-as-string-changed', listenOnce = function(){
//this.colorName = this.$.picker.immediateColorAsString;
this.$.picker.setColor();
this.$.picker.removeEventListener('immediate-color-as-string-changed', listenOnce);
}.bind(this));
}
},
openColorPicker: function(){
this.$.picker.open();
},
_computeBackgroundColor: function(){
return 'background: rgb('+this.value.red+', '+this.value.green+','+this.value.blue+')';
},
_isValueDefined: function(){
return this.value.red >= 0 && this.value.green >= 0 && this.value.blue >= 0;
}
});
</script>
</dom-module>
Loading

0 comments on commit 7d383d6

Please sign in to comment.