palette.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.ui.Palette &amp; goog.ui.ColorPalette</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.array');
  13. goog.require('goog.color');
  14. goog.require('goog.debug.DivConsole');
  15. goog.require('goog.debug.LogManager');
  16. goog.require('goog.dom.classlist');
  17. goog.require('goog.events');
  18. goog.require('goog.log');
  19. goog.require('goog.object');
  20. goog.require('goog.ui.ColorPalette');
  21. goog.require('goog.ui.Component.EventType');
  22. goog.require('goog.ui.CustomColorPalette');
  23. goog.require('goog.ui.Palette');
  24. goog.require('goog.dom.TagName');
  25. </script>
  26. <link rel="stylesheet" href="css/demo.css">
  27. <link rel="stylesheet" href="../css/palette.css">
  28. <style>
  29. #cp_text, #ccp_text {
  30. font: medium monospace;
  31. }
  32. .fakelink {
  33. text-decoration: underline;
  34. color: blue;
  35. cursor: pointer;
  36. }
  37. /* Demo styles */
  38. .simple-palette .goog-palette-active {
  39. border-color: #edf;
  40. }
  41. .simple-palette .goog-palette-table {
  42. border: 1px solid #888;
  43. border-collapse: separate;
  44. }
  45. .simple-palette .goog-palette-cell {
  46. height: 16px;
  47. width: 16px;
  48. margin: 0;
  49. border: 0;
  50. padding: 1px;
  51. text-align: center;
  52. vertical-align: middle;
  53. }
  54. .simple-palette .goog-palette-cell-hover {
  55. border: 1px solid #cc0;
  56. padding: 0;
  57. background-color: #ff0 !important;
  58. }
  59. .simple-palette .goog-palette-cell-selected {
  60. border: 1px solid #008 !important;
  61. padding: 0;
  62. color: #00f;
  63. font-weight: bold;
  64. }
  65. .goog-palette-colorswatch {
  66. position: relative;
  67. height: 18px;
  68. width: 18px;
  69. font-size: x-small;
  70. }
  71. .goog-palette-cell-hover .goog-palette-colorswatch {
  72. height: 16px;
  73. width: 16px;
  74. border: 1px solid #FFF;
  75. }
  76. .goog-palette-cell-selected .goog-palette-colorswatch {
  77. height: 16px;
  78. width: 16px;
  79. border: 1px solid #333;
  80. color: #fff;
  81. font-weight: bold;
  82. background-image: url(../images/color-swatch-tick.gif);
  83. background-repeat: no-repeat;
  84. background-position: center center;
  85. }
  86. .goog-palette-customcolor {
  87. position: relative;
  88. height: 16px;
  89. width: 16px;
  90. font-size: x-small;
  91. background-color: #FAFAFA;
  92. color: #666;
  93. border: 1px solid #EEE;
  94. }
  95. .goog-palette-cell-hover .goog-palette-customcolor {
  96. background-color: #FEE;
  97. border: 1px solid #F66;
  98. color: #F66;
  99. }
  100. </style>
  101. </head>
  102. <body>
  103. <h1>goog.ui.Palette &amp; goog.ui.ColorPalette</h1>
  104. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  105. <tbody>
  106. <tr valign="top">
  107. <td width="67%">
  108. <fieldset>
  109. <legend>Demo of the <strong>goog.ui.Palette</strong>:</legend>
  110. <br/>
  111. <label id="p1">This is a 3x3 palette with only 8 elements:</label>
  112. <label>You selected <span id="p1_value">nothing</span></label>
  113. <br/>
  114. <i>Note that if you don't specify any dimensions, the palette will auto-size
  115. to fit your items in the smallest square.</i><br/>
  116. </fieldset>
  117. <br/>
  118. <br/>
  119. <fieldset>
  120. <legend>Demo of the <strong>goog.ui.ColorPalette</strong>:</legend>
  121. <div id="cp"></div>
  122. <p>The color you selected was:
  123. <span id="cp_value" class="goog-inline-block goog-palette-colorswatch">
  124. &nbsp;
  125. </span>
  126. <span id="cp_text"></span>
  127. </p>
  128. </fieldset>
  129. <br/>
  130. <br/>
  131. <fieldset>
  132. <legend>Demo of the <strong>goog.ui.CustomColorPalette</strong>:</legend>
  133. <div id="ccp"></div>
  134. <p>The color you selected was:
  135. <span id="ccp_value" class="goog-inline-block goog-palette-colorswatch">
  136. &nbsp;
  137. </span>
  138. <span id="ccp_text"></span>
  139. </p>
  140. </fieldset>
  141. </td>
  142. <td width="33%">
  143. <!-- Event log. -->
  144. <fieldset class="goog-debug-panel">
  145. <legend>Event Log</legend>
  146. <div id="log"></div>
  147. </fieldset>
  148. </td>
  149. </tr>
  150. </tbody>
  151. </table>
  152. <br/>
  153. <div id="perf"></div>
  154. <script>
  155. var timer = goog.now();
  156. // Set up a logger.
  157. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  158. var logger = goog.log.getLogger('demo');
  159. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  160. logconsole.setCapturing(true);
  161. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  162. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  163. function logEvent(e) {
  164. var component = e.target;
  165. var caption = (typeof component.getCaption == 'function' &&
  166. component.getCaption()) || component.getId();
  167. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  168. }
  169. var items = [];
  170. for (var i = 1; i < 9; i++) {
  171. items.push(goog.dom.createTextNode('' + i));
  172. }
  173. var p1 = new goog.ui.Palette(items);
  174. p1.render(goog.dom.getElement('p1'));
  175. goog.dom.classlist.add(p1.getElement(), 'simple-palette');
  176. goog.events.listen(p1, goog.ui.Component.EventType.ACTION,
  177. function(e) {
  178. var palette = e.target;
  179. goog.dom.setTextContent(goog.dom.getElement('p1_value'),
  180. goog.dom.getTextContent(palette.getSelectedItem()));
  181. });
  182. goog.events.listen(p1, EVENTS, logEvent);
  183. // Color Palette Examples
  184. function createColorPaletteDemo(colors, width, caption) {
  185. goog.dom.getElement('cp').appendChild(goog.dom.createDom(goog.dom.TagName.P, null, caption));
  186. var cp = new goog.ui.ColorPalette(colors);
  187. cp.setSize(width); // If we only set the columns, the rows are calculated.
  188. cp.render(goog.dom.getElement('cp'));
  189. goog.events.listen(cp, goog.ui.Component.EventType.ACTION, onColorEvent);
  190. goog.events.listen(cp, EVENTS, logEvent);
  191. return cp;
  192. }
  193. function onColorEvent(e) {
  194. var palette = e.target;
  195. var color = palette.getSelectedColor();
  196. goog.style.setStyle(goog.dom.getElement('cp_value'), 'background-color',
  197. color);
  198. goog.dom.getElement('cp_value').title = color;
  199. goog.dom.getElement('cp_text').innerHTML = goog.color.parse(color).hex;
  200. }
  201. createColorPaletteDemo(['black', 'blue', 'red', 'magenta',
  202. 'green', 'cyan', 'orange', 'yellow', '#404040', '#808080', '#b0b0b0',
  203. 'white'], 4, 'This is a 4x3 color palette with named colors:');
  204. createColorPaletteDemo(
  205. ['#F00', '#F90', '#FF0', '#3F3', '#0FF', '#00F', '#90F', '#F0F'], 8,
  206. 'These colors are bright:');
  207. createColorPaletteDemo([
  208. '#EA9999', '#F9CB9C', '#FFE599', '#B6D7A8',
  209. '#A2C4C9', '#9FC5E8', '#B4A7D6', '#D5A6BD',
  210. '#E06666', '#F6B26B', '#FFD966', '#93C47D',
  211. '#76A5AF', '#6FA8DC', '#8E7CC3', '#C27BA0',
  212. '#CC0000', '#E69138', '#F1C232', '#6AA84F',
  213. '#45818E', '#3D85C6', '#674EA7', '#A64D79'],
  214. 8, 'This is a lovely pastelle color palette:');
  215. var cpg = createColorPaletteDemo(['#000', '#444', '#666', '#999', '#AAA',
  216. '#CCC', '#EEE', '#FFF'], 8,
  217. 'This is a grey scale color palette:');
  218. // Hack for the demo, don't try this at home, kids!
  219. var cpc = goog.dom.getElement('cp');
  220. cpc.appendChild(document.createTextNode('Change: '));
  221. cpc.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN,
  222. {'class': 'fakelink', 'onclick': function() {
  223. cpg.setColors(
  224. ['#000', '#400', '#600', '#900', '#A00', '#C00', '#E00', '#F00']);
  225. }}, 'Reds'));
  226. cpc.appendChild(document.createTextNode(', '));
  227. cpc.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN,
  228. {'class': 'fakelink', 'onclick': function() {
  229. cpg.setColors(
  230. ['#000', '#040', '#060', '#090', '#0A0', '#0C0', '#0E0', '#0F0']);
  231. }}, 'Greens'));
  232. cpc.appendChild(document.createTextNode(', '));
  233. cpc.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN,
  234. {'class': 'fakelink', 'onclick': function() {
  235. cpg.setColors(
  236. ['#000', '#004', '#006', '#009', '#00A', '#00C', '#00E', '#00F']);
  237. }}, 'Blues'));
  238. cpc.appendChild(document.createTextNode(', '));
  239. cpc.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN,
  240. {'class': 'fakelink', 'onclick': function() {
  241. cpg.setColors(
  242. ['#000', '#444', '#666', '#999', '#AAA', '#CCC', '#EEE', '#FFF']);
  243. }}, 'Greys'));
  244. // Custom Color Palette Examples
  245. var cp = new goog.ui.CustomColorPalette(['#FE1', '#ACD' , '#119']);
  246. cp.setSize(6);
  247. cp.render(goog.dom.getElement('ccp'));
  248. goog.dom.classlist.add(cp.getElement(), 'color-picker');
  249. goog.events.listen(cp, goog.ui.Component.EventType.ACTION, function (e) {
  250. var palette = e.target;
  251. var color = palette.getSelectedColor();
  252. goog.style.setStyle(goog.dom.getElement('ccp_value'), 'background-color',
  253. color);
  254. goog.dom.getElement('ccp_value').title = color;
  255. goog.dom.getElement('ccp_text').innerHTML =
  256. goog.color.parse(color).hex;
  257. });
  258. goog.events.listen(cp, EVENTS, logEvent);
  259. // Perf and clean up
  260. goog.dom.setTextContent(goog.dom.getElement('perf'),
  261. (goog.now() - timer) + 'ms');
  262. </script>
  263. </body>
  264. </html>