| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 typedef void PickerListener(String selectedColor); | 5 typedef void PickerListener(String selectedColor); |
| 6 | 6 |
| 7 class ColorPicker { | 7 class ColorPicker { |
| 8 | 8 |
| 9 static final hexValues = const ['00', '33', '66', '99', 'CC', 'FF']; | 9 static final hexValues = const ['00', '33', '66', '99', 'CC', 'FF']; |
| 10 static final COLS = 18; | 10 static final COLS = 18; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 showSelected(); | 35 showSelected(); |
| 36 fireSelected(); | 36 fireSelected(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void onMouseMove(MouseEvent event) { | 39 void onMouseMove(MouseEvent event) { |
| 40 int x = event.offsetX; | 40 int x = event.offsetX; |
| 41 int y = event.offsetY - 40; | 41 int y = event.offsetY - 40; |
| 42 if (( y < 0) || (x >= width)) { | 42 if (( y < 0) || (x >= width)) { |
| 43 return; | 43 return; |
| 44 } | 44 } |
| 45 ctx.setFillStyle(getHexString(getColorIndex(x, y))); | 45 ctx.fillStyle = getHexString(getColorIndex(x, y)); |
| 46 ctx.fillRect(0, 0, width/2, 30); | 46 ctx.fillRect(0, 0, width/2, 30); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void onMouseDown(MouseEvent event) { | 49 void onMouseDown(MouseEvent event) { |
| 50 Element elt = event.target; | 50 Element elt = event.target; |
| 51 event.cancelBubble = true; | 51 event.cancelBubble = true; |
| 52 int x = event.offsetX; | 52 int x = event.offsetX; |
| 53 int y = event.offsetY - 40; | 53 int y = event.offsetY - 40; |
| 54 if ((y < 0) || (x >= width)) | 54 if ((y < 0) || (x >= width)) |
| 55 return; | 55 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 canvasElement.onmousemove = (e) { onMouseMove(e); }; | 67 canvasElement.onmousemove = (e) { onMouseMove(e); }; |
| 68 canvasElement.onmousedown = (e) { onMouseDown(e); }; | 68 canvasElement.onmousedown = (e) { onMouseDown(e); }; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void drawPalette() { | 71 void drawPalette() { |
| 72 int i=0; | 72 int i=0; |
| 73 for (int r=0; r < 256; r+=51) { | 73 for (int r=0; r < 256; r+=51) { |
| 74 for (int g=0; g < 256; g+=51) { | 74 for (int g=0; g < 256; g+=51) { |
| 75 for (int b=0; b < 256; b+=51) { | 75 for (int b=0; b < 256; b+=51) { |
| 76 String color = getHexString(i); | 76 String color = getHexString(i); |
| 77 ctx.setFillStyle(color); | 77 ctx.fillStyle = color; |
| 78 int x = BW * (i % COLS); | 78 int x = BW * (i % COLS); |
| 79 int y = BH * (i ~/ COLS) + 40; | 79 int y = BH * (i ~/ COLS) + 40; |
| 80 ctx.fillRect(x + BP, y + BP, BW - 2 * BP, BH - 2 * BP); | 80 ctx.fillRect(x + BP, y + BP, BW - 2 * BP, BH - 2 * BP); |
| 81 i++; | 81 i++; |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 void fireSelected() { | 87 void fireSelected() { |
| 88 for (final listener in _listeners) { | 88 for (final listener in _listeners) { |
| 89 listener(_selectedColor); | 89 listener(_selectedColor); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 int getColorIndex(int x, int y) { | 93 int getColorIndex(int x, int y) { |
| 94 // Get color index 0-215 using row, col | 94 // Get color index 0-215 using row, col |
| 95 int i = y ~/ BH * COLS + x ~/ BW; | 95 int i = y ~/ BH * COLS + x ~/ BW; |
| 96 return i; | 96 return i; |
| 97 } | 97 } |
| 98 | 98 |
| 99 void showSelected() { | 99 void showSelected() { |
| 100 ctx.setFillStyle(_selectedColor); | 100 ctx.fillStyle = _selectedColor; |
| 101 ctx.fillRect(width / 2, 0, width / 2, 30); | 101 ctx.fillRect(width / 2, 0, width / 2, 30); |
| 102 ctx.setFillStyle("white"); | 102 ctx.fillStyle = "white"; |
| 103 ctx.fillRect(0, 0, width / 2, 30); | 103 ctx.fillRect(0, 0, width / 2, 30); |
| 104 } | 104 } |
| 105 | 105 |
| 106 String getHexString(int i) { | 106 String getHexString(int i) { |
| 107 int r = i ~/ 36; | 107 int r = i ~/ 36; |
| 108 int g = (i % 36) ~/ 6; | 108 int g = (i % 36) ~/ 6; |
| 109 int b = i % 6; | 109 int b = i % 6; |
| 110 return '#${hexValues[r]}${hexValues[g]}${hexValues[b]}'; | 110 return '#${hexValues[r]}${hexValues[g]}${hexValues[b]}'; |
| 111 } | 111 } |
| 112 | 112 |
| 113 } | 113 } |
| OLD | NEW |