Get rgb values from color block


#1

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
49

54

50


#2

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


#3

thanks a lot ! , it’s exists some JS developer guide?


#4

I have problems when i’m put your solution in my code section :

Setup Section:
setupSection

Declare Section:
declareSection
Code Section:
CodeSection
And i have the follow error when i’m traying to load my own board

what can i do to solve this?


#5

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


#6

hi thanks for your response but i got the next error :sleepy:

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:

09

and

20

plz help :C


#7

hi thanks for your response but i got the next error :sleepy:

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:

09
and
20

plz help :C


#8

Hi epsilon11101,

Can you please remove the “function”?

just like what you did before:

but only change the last line to:

image

let me know if it works.

Best,

Allen


#9

Hi Allen i had the next error :frowning:

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 |


#10

Hi Allen i had the next error :frowning:

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 |


#11

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…:joy:

Feel free to continue our debugging process and I’ll be happy to help.

getColor.mext.zip (12.8 KB)


#12

Hi Allen finally works , the error was in the first line

(arg) changed with (args)

lol!! only the s! , thank’s a lot!


#13

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