Hi, im develop my own blocks and i want to get RGB values from Color Block
it’s any way to convert HEX format to RGB values ?
for example #FF0000 -> R:255 G:0 B:0
Hi, im develop my own blocks and i want to get RGB values from Color Block
it’s any way to convert HEX format to RGB values ?
for example #FF0000 -> R:255 G:0 B:0
Hi epsilon11101,
I’ve got a perfect solution for you:
Actually, you can write either JavaScript or Arduino C in the code section. The only difference is that, originally, you wrote Arduino C code; now, you write JS code, but the JS code has to be a function that returns the Arduino C code string for further compilation and execution.
So what we need to do here is to write a JS function instead, to convert the hex code into RGB code first, and then pass the RGB value as an input parameter into the Arduino C function you used originally.
Here is the code based on your case:
(arg)=>{
let color = arg.color; // for example, #ffaabb. This is the JavaScript way to get the input color value from the blog.
let r = parseInt(‘0x’+color.substr(1,2)); // translate the color hex code into rgb code
let g = parseInt(‘0x’+color.substr(3,2));
let b = parseInt(‘0x’+color.substr(5,2));
return getValue(${r},${g},${b});
// return the rgb value into your original function as input.
}
Hope this could be helpful for you.
Please let me know if you have further question.
Best,
Allen
I have problems when i’m put your solution in my code section :
Setup Section:
Declare Section:
Code Section:
And i have the follow error when i’m traying to load my own board
what can i do to solve this?
Hi epsilon11101,
Sorry I made a mistake yesterday. The last line of code should be
The back quote was deleted automatically by this forum’s editor…So I just show you with this screenshot. Please don’t forget to add the back quote to wrap the whole returned function as they’re essentially just string.
We haven’t had documentations for JavaScript usage in this part. But thanks for reminding us, we’ll add it up soon.
Best,
Allen
hi thanks for your response but i got the next error
unknown: Unexpected token, expected “,” (3604:36)
3602 | “arduinoc”: {
3603 | “code”: function (arg)=>{\nlet color = arg.color; \nlet r = parseInt(‘0x’+color.substr(1,2)); \nlet g = parseInt(‘0x’+color.substr(3,2));\nlet b = parseInt(‘0x’+color.substr(5,2));\nreturn
3604 | getValues($ {
| ^
3605 | r
3606 | }, $ {
3607 |
This is mi code i thought that i forgot “;” in return statment and i was traying with both but they had the same error:
and
plz help :C
hi thanks for your response but i got the next error
unknown: Unexpected token, expected “,” (3604:36)
3602 | “arduinoc”: {
3603 | “code”: function (arg)=>{\nlet color = arg.color; \nlet r = parseInt(‘0x’+color.substr(1,2)); \nlet g = parseInt(‘0x’+color.substr(3,2));\nlet b = parseInt(‘0x’+color.substr(5,2));\nreturn
3604 | getValues($ {
| ^
3605 | r
3606 | }, $ {
3607 |
This is mi code i thought that i forgot “;” in return statment and i was traying with both but they had the same error:
and
plz help :C
Hi epsilon11101,
Can you please remove the “function”?
just like what you did before:
but only change the last line to:
let me know if it works.
Best,
Allen
Hi Allen i had the next error
unknown: Unexpected token, expected “,” (3604:36)
3602 | “arduinoc”: {
3603 | “code”: (arg)=>{\nlet color = arg.color; \nlet r = parseInt(‘0x’+color.substr(1,2)); \nlet g = parseInt(‘0x’+color.substr(3,2));\nlet b = parseInt(‘0x’+color.substr(5,2));\nreturn
3604 | getValues($ {
| ^
3605 | r
3606 | }, $ {
3607 |
Hi Allen i had the next error
unknown: Unexpected token, expected “,” (3604:36)
3602 | “arduinoc”: {
3603 | “code”: (arg)=>{\nlet color = arg.color; \nlet r = parseInt(‘0x’+color.substr(1,2)); \nlet g = parseInt(‘0x’+color.substr(3,2));\nlet b = parseInt(‘0x’+color.substr(5,2));\nreturn
3604 | getValues($ {
| ^
3605 | r
3606 | }, $ {
3607 |
Hi epsilon11101,
Could you please try this extension file we provide and see if it works? It’s hard for me to tell where was wrong from the error message…
Feel free to continue our debugging process and I’ll be happy to help.
getColor.mext.zip (12.8 KB)
Hi Allen finally works , the error was in the first line
(arg) changed with (args)
lol!! only the s! , thank’s a lot!
Hi epsilon11101,
I’m so sorry for my negligence…
And thanks for letting me that you fix this issue. Hope you could submit your extension for us to review and publish it in the extension market.
Best,
Allen
How do you install this extension? I downloaded the zip file but could not add to extension. Thanks