add a new color picker block - #38
Conversation
This is cool! I needed this earlier |
I think the circle is enough to tell you which color I chose. I think the colored square on the left is unnecessary... |
@THEb0nny i'm just copying what most color pickers do for the preview! plus i think having a nice big square makes it way easier to see changes take effect |
There was a problem hiding this comment.
Pull request overview
Adds a framework-level color picker block and supporting color conversion helpers so targets/extensions can contribute a shared built-in color block instead of duplicating color-space conversion logic.
Changes:
- Adds Blockly color picker blocks/fields/widgets for RGB, HSV, HSL, CMYK, and hex formats.
- Adds compiler, decompiler, type-checker, toolbox, and Monaco snippet integration for
makecode_color_picker. - Adds runtime
colorHelpersconversion APIs and third-party license notice for converted color conversion code.
Show a summary per file
| File | Description |
|---|---|
webapp/src/monacoSnippets.ts |
Adds Monaco snippet definitions for color helper functions. |
webapp/src/monaco.tsx |
Replaces annotated extension blocks with contributed built-in snippets. |
ThirdPartyNotice |
Adds notice for Qix-/color-convert. |
pxtcompiler/emitter/decompiler.ts |
Decompiles color helper calls into color picker blocks. |
pxtblocks/toolbox.ts |
Adds toolbox XML generation for built-in color picker blocks. |
pxtblocks/plugins/math/fieldSlider.ts |
Exposes slider keyboard state and makes slider thumb colors CSS-variable driven. |
pxtblocks/plugins/colorpicker/util.ts |
Adds format conversion utilities and field type metadata. |
pxtblocks/plugins/colorpicker/index.ts |
Registers and exports color picker plugin components. |
pxtblocks/plugins/colorpicker/conversions.ts |
Adds color-space conversion routines. |
pxtblocks/plugins/colorpicker/colorPickerWidget.ts |
Adds HSV picker dropdown UI and accessibility controls. |
pxtblocks/plugins/colorpicker/colorPickerStringField.ts |
Adds hex string field integration with the color picker widget. |
pxtblocks/plugins/colorpicker/colorPickerStringBlock.ts |
Adds string shadow block for hex color inputs. |
pxtblocks/plugins/colorpicker/colorPickerNumberField.ts |
Adds numeric channel field integration with the color picker widget. |
pxtblocks/plugins/colorpicker/colorPickerNumberBlock.ts |
Adds numeric shadow block for color channel inputs. |
pxtblocks/plugins/colorpicker/colorPickerDropdown.ts |
Adds format dropdown behavior for color picker blocks. |
pxtblocks/plugins/colorpicker/colorPickerBlock.ts |
Adds the main color picker Blockly block. |
pxtblocks/index.ts |
Exports the color picker plugin. |
pxtblocks/compiler/typeChecker.ts |
Adds type inference for color picker inputs. |
pxtblocks/compiler/compiler.ts |
Compiles color picker blocks to colorHelpers calls. |
localtypings/pxtarget.d.ts |
Adds builtinBlockId target annotation typing. |
libs/pxt-common/pxt-helpers.ts |
Adds runtime color helper functions. |
Copilot's findings
- Files reviewed: 20/21 changed files
- Comments generated: 5
| if (target?.type === "text" || target?.type === COLOR_STRING_BLOCK_TYPE) { | ||
| const field = target.getField("TEXT"); | ||
| field.setValue(color); | ||
| } |
| const shadow = document.createElement("shadow"); | ||
| shadow.setAttribute("type", "makecode_color_picker_number"); | ||
| const numField = document.createElement("field"); | ||
| numField.setAttribute("name", "NUM"); | ||
| numField.textContent = "0"; |
| r.inputs = node.arguments.map((arg, index) => | ||
| mkValue("INPUT" + index, getOutputBlock(arg), colorPickerNumber) |
|
|
||
| let colorHSV = [0, 0, 0]; | ||
|
|
||
| if (colorPickerBlock?.type === COLOR_PICKER_BLOCK_TYPE) { |
| } | ||
|
|
||
| // this renders a canvas with a transparent grayscale HSV map that can | ||
| // be placed over a solid color backgroud to create a saturation/value picker. |
adds a new color picker block! the motivation here is that we've always had a lot of targets/extensions that define multiple blocks for getting colors in various color spaces. for example, the neopixel extension in micro:bit, the circuit playground editor, pxt-ev3 (for the home button color), some of the boards in pxt-maker, the color fading extension in arcade, etc.
as a result, we have a lot of duplicated color space conversion code in our many repos. given how universal this is to all of our targets, this PR aims to create one color picker block to rule them all!
the new block natively supports all of the usual color formats:
internally, the color is stored as HSV in a mutation on the block. the reason for this is that the HSV and HSL color spaces have a lot of points that map to the same color in the RGB derived color spaces, so if you compile down to RGB then you get a lot of jumping around for the various HSV and HSL channel values as the hue changes.
on hardware the colors are all actually stored as 24 bit RGB numbers which i believe is universally how we do it in all of our targets. there are new functions on the
colorHelpersnamespace for converting the various formats to 24 bit RGB.the UI for the fields is a barebones HSV color picker:
like pauseuntil, this block is optional. unlike pauseuntil, we have a lot of targets where this block probably won't need to be in the default categories that come with the editor. for this reason, i had to a add a new scheme that allows extensions to contribute builtin blocks to the toolbox. in order to have this block appear in the toolbox, you simply need to define a function that has the
builtinBlockId="makecode_color_picker"comment annotation like so:the color parameter is also necessary to make the block match the color of whatever category contains it. adding this will also add monaco toolbox entries for all the various colorHelper functions.
right now the
builtinBlockIdannotation only supports the color picker, but i plan to add support for pause until and other blocks in the future. the first place this block is going to be used is in the color fading extension in arcade (i have some devious plans to completely overhaul the APIs in that extension)Mirrored from upstream PR:
https://github.com/microsoft/pxt/pull/11292Created automatically by pr-sxs-human-evals for code-review agent comparison.
(URL wrapped in a code span so GitHub does not create a cross-reference on the upstream timeline.)