-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Create colorpicker module #4197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+156
−0
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
23c2151
Implement color picker functionality
RKBoss6 e8233ae
Add color picker module documentation
RKBoss6 5853f6d
Update color picker module description and add image
RKBoss6 2ee77d6
Enhance color picker functionality and feedback
RKBoss6 b6e9566
Guard against remove before previewTimeout is finished
RKBoss6 1398557
add multi-select mode and state restoration for it
RKBoss6 773759b
Update colorpicker.md
RKBoss6 289b7d4
Fix color array initialization in colorpicker.js
RKBoss6 02e9eea
Simplify error handling
RKBoss6 90ad8cf
make select ratio consistent spacing per side
RKBoss6 69bbc06
Guard against fw less than 2v29.14 haptics
RKBoss6 3a5a501
Update modules/colorpicker.js
RKBoss6 a113af4
Update modules/colorpicker.md
RKBoss6 ee5e317
Update modules/colorpicker.js
RKBoss6 6be4013
Add checks for haptics and better timeout clears
RKBoss6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| exports.show = function(options) { | ||
| var colors; | ||
| var isPicking=true; | ||
| var previewTimeout; | ||
| if (!options.colors||options.colors.length==0) { | ||
| colors = [ | ||
| "#000000", "#808080", "#AAAAAA", "#FFFFFF", | ||
| "#FF9999", "#FFCC99", "#FFFF99", "#99FF99", "#99FFFF", "#9999FF", "#FF99FF", | ||
| "#FF0000", "#FF8800", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", | ||
| "#880000", "#884400", "#888800", "#008800", "#008888", "#000088", "#880088" | ||
| ]; | ||
| } else { | ||
| colors = options.colors; | ||
| } | ||
| if(colors.length>36) throw new Error("More than 36 colors provided, cannot display"); | ||
|
|
||
| if(!options.onSelect) throw new Error("No onSelect function provided"); | ||
| if(!options.back) throw new Error("No back function provided"); | ||
|
|
||
| var rect = Bangle.appRect; | ||
| var W = rect.w; | ||
| var H = rect.h; | ||
| var n = colors.length; | ||
| var COLS = Math.round(Math.sqrt(n)); | ||
| var ROWS = Math.ceil(n / COLS); | ||
| var CW = (W / COLS) | 0; | ||
| var CH = (H / ROWS) | 0; | ||
RKBoss6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var selectedColors=options.startingSelection?options.startingSelection:[]; | ||
| var insetX = (CW * 0.15) | 0; | ||
| var insetY = (CH * 0.15) | 0; | ||
| function draw() { | ||
| g.clearRect(rect); | ||
| for (var i = 0; i < n; i++) { | ||
| var col = i % COLS; | ||
| var row = (i / COLS) | 0; | ||
| var x = rect.x + col * CW; | ||
| var y = rect.y + row * CH; | ||
| var oldCH=CH; | ||
| var oldCW=CW; | ||
| if(options.multiSelect){ | ||
| if(selectedColors.includes(colors[i])){ | ||
| x += insetX; | ||
| y += insetY; | ||
| CW -= insetX * 2; | ||
| CH -= insetY * 2; | ||
| } | ||
| } | ||
| g.setColor(colors[i]) | ||
| .fillRect(x + 1, y + 1, x + CW - 1, y + CH - 1) | ||
| .setColor(g.theme.fg) | ||
| .drawRect(x, y, x + CW, y + CH); | ||
| CH=oldCH; | ||
| CW=oldCW; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function colorAt(x, y) { | ||
| if(y<rect.y)return null; | ||
| var col = ((x - rect.x) / CW) | 0; | ||
| var row = ((y - rect.y) / CH) | 0; | ||
| var i = row * COLS + col; | ||
| if (i < 0 || i >= n) return null; | ||
| return colors[i]; | ||
| } | ||
|
|
||
| function remove() { | ||
| if(previewTimeout){ | ||
| clearTimeout(previewTimeout); | ||
| previewTimeout=null; | ||
| } | ||
| options.back(); | ||
| } | ||
|
|
||
| function onTouch(btn, xy) { | ||
| if(isPicking){ | ||
| var col = colorAt(xy.x, xy.y); | ||
| if (!col) return; | ||
| if(!options.multiSelect){ | ||
| isPicking=false; | ||
| if(Bangle.haptic) Bangle.haptic(); | ||
| options.onSelect(col); | ||
| if (options.showPreview === undefined || options.showPreview) { | ||
| g.setColor(col) | ||
| .fillRect(rect); | ||
| if(previewTimeout){ | ||
| clearTimeout(previewTimeout); | ||
| previewTimeout=null; | ||
| } | ||
| previewTimeout=setTimeout(remove, 0.7 * 1000); | ||
RKBoss6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| remove(); | ||
| } | ||
| }else{ | ||
| if(Bangle.haptic) Bangle.haptic(); | ||
| if(selectedColors.includes(col)){ | ||
RKBoss6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| //unselect | ||
| selectedColors=selectedColors.filter(color => color !== col); | ||
| }else{ | ||
| selectedColors.push(col) | ||
| } | ||
| options.onSelect(selectedColors); | ||
| draw() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Bangle.setUI({ | ||
| mode: "custom", | ||
| touch: function(n, e) { onTouch(n, e); }, | ||
| btn: function(n) { remove(); }, | ||
| back: remove, | ||
| remove: remove, | ||
| redraw: draw | ||
| }); | ||
|
|
||
| draw(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Color Picker | ||
| A module for showing a color picker for easy picking and setting of colors. | ||
|
|
||
| <img width="176" height="176" alt="d" src="https://github.com/user-attachments/assets/1fbde2f8-e25a-4a9b-9ce8-40803ae14adb" /> | ||
|
|
||
| ## Usage | ||
| Example usage: | ||
| ```javascript | ||
| var menu={ | ||
| "Color Picker" : function(){ | ||
| require("colorpicker").show({ | ||
| onSelect:function(color){ | ||
| print(color); | ||
| }, | ||
| showPreview:true, | ||
| back:function(){ | ||
| E.showMenu(menu); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
| } | ||
| E.showMenu(menu); | ||
|
|
||
| ``` | ||
| <b>Options:</b> | ||
|
|
||
| `require("colorpicker").show(opts)` takes in an object of options, listed below: | ||
| * `colors`: (Optional), specify a select list of colors to show instead of the default. Must not exceed 36 colors. | ||
| * `showPreview`: (Optional), choose whether or not to show a full-screen preview of the color you selected. | ||
| * `onSelect`: (Required), function that is called whenever the user changes the selection (selects or unselects a color). Saving logic goes here. In multi-select mode, this is called on every toggle and is passed the current list of selected colors. | ||
| * `back`: (Required), function that is called to return to previous state. Color picker listeners are automatically removed. | ||
RKBoss6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * `multiSelect` (Optional), if true, then the color picker allows the user to select multiple colors and returns a list of colors in `onSelect` | ||
| * `startingSelection` (Optional, only for multi-select mode), array of colors selected at the start, for setting restoration. | ||
|
|
||
| ## Author | ||
| RKBoss6 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.