Discuss Scratch

KPWOLF
Scratcher
3 posts

Detect Color by Number

Hello everyone.

I think it would be very useful to have a separate sensor block to detect color by an inputted number instead of just selecting a color with the eye dropper. I'm not saying the eye dropper should be removed, i just want another block to detect color by number. Here is an example of how it could be helpful: A while ago I thought it would be cool to make a program that would condense a stage into pen code so that it could be printed out again in pen. What I found was that this would be way too difficult for me because I did not want to brute force 9000+ color combinations. I gave up on the project. In situations similar to this one and even others, it could be very helpful to have RGB or some other number combination to detect colors instead of having to use the eyedropper every time.

- KPWOLF
jakel181
Scratcher
1000+ posts

Detect Color by Number

Support! I would love RGB detection
TheLogFather
Scratcher
1000+ posts

Detect Color by Number

You can drop an expression into the colour-square, and it treats it as a 24 bit RGB value:
touchingcolorjoin0xff0000? red touchingcolor256*255? green touchingcolor65536*255+256*255? yellow touchingcolorvariable? whatever you want, on-the-fly

Note that Scratch only matches the first five bits of red & green components, and first four bits of blue.

That means 0xFFFFFF (i.e. white) will match 0xF8F8F0 (off-white), for example.

(Consequently, there are only up to 32*32*16 checks needed to find a match for a given pixel, rather than the full 256*256*256.)


Similar thing for the “set pen color” block, BTW (the one with the colour square, not the one with slot for typing a number – and it's clearly not ‘matching’ a colour here, so all bits make a difference):
setpencolortojoin0x00ffff cyan

..except, it can also take a 32 bit ARGB value – i.e. with alpha in the highest order byte:
setpencolortojoin0x40ff00ff magenta, with 75% 'ghosting'

Last edited by TheLogFather (April 19, 2017 16:49:15)

NitroCipher
Scratcher
500+ posts

Detect Color by Number

TheLogFather wrote:

You can drop an expression into the colour-square, and it treats it as a 24 bit RGB value:
touchingcolorjoin0xff0000? red touchingcolor256*255? green touchingcolor65536*255+256*255? yellow touchingcolorvariable? whatever you want, on-the-fly

Note that Scratch only matches the first five bits of red & green components, and first four bits of blue.

That means 0xFFFFFF (i.e. white) will match 0xF8F8F0 (off-white), for example.

(Consequently, there are only up to 32*32*16 checks needed to find a match for a given pixel, rather than the full 256*256*256.)


Similar thing for the “set pen color” block, BTW (the one with the colour square, not the one with slot for typing a number – and it's clearly not ‘matching’ a colour here, so all bits make a difference):
setpencolortojoin0x00ffff cyan

..except, it can also take a 32 bit ARGB value – i.e. with alpha in the highest order byte:
setpencolortojoin0x40ff00ff magenta, with 75% 'ghosting'

Semi offtopic but if you have the time, would you be able to place this information on the scratch wiki?
jromagnoli
Scratcher
1000+ posts

Detect Color by Number

KPWOLF wrote:

Hello everyone.

I think it would be very useful to have a separate sensor block to detect color by an inputted number instead of just selecting a color with the eye dropper. I'm not saying the eye dropper should be removed, i just want another block to detect color by number. Here is an example of how it could be helpful: A while ago I thought it would be cool to make a program that would condense a stage into pen code so that it could be printed out again in pen. What I found was that this would be way too difficult for me because I did not want to brute force 9000+ color combinations. I gave up on the project. In situations similar to this one and even others, it could be very helpful to have RGB or some other number combination to detect colors instead of having to use the eyedropper every time.

- KPWOLF
You can use this:
setrto0repeat32setgto0repeat32setbto0repeat16iftouchingcolorr*65536+g*256+bthenaddr*65536+g*256+btocolor scanchangebby16changegby8changerby8ortouchingcolorjoin0xfffff?
amaquate
Scratcher
100+ posts

Detect Color by Number

You can do this too!
whenclickediftouchingcolorsetthecoloridtothisvariable]?thenmove20steps
(That was just an example
L115a3uk
Scratcher
5 posts

Detect Color by Number

Could you create a piece of code that detects a certain range of colors? that way you would only need to search 256 or whatever rather than tens of thousands?
qaz1550
Scratcher
100+ posts

Detect Color by Number

TheLogFather wrote:

You can drop an expression into the colour-square, and it treats it as a 24 bit RGB value:
touchingcolorjoin0xff0000? red touchingcolor256*255? green touchingcolor65536*255+256*255? yellow touchingcolorvariable? whatever you want, on-the-fly

Note that Scratch only matches the first five bits of red & green components, and first four bits of blue.

That means 0xFFFFFF (i.e. white) will match 0xF8F8F0 (off-white), for example.

(Consequently, there are only up to 32*32*16 checks needed to find a match for a given pixel, rather than the full 256*256*256.)


Similar thing for the “set pen color” block, BTW (the one with the colour square, not the one with slot for typing a number – and it's clearly not ‘matching’ a colour here, so all bits make a difference):
would this work for touching if a colour is touching the same colour in a custom block?
earlier on in the code:
setpencolortocolour
later in the code:
colorcolouristouchingcolour?
victerhh
Scratcher
9 posts

Detect Color by Number

colorcoloristouchingcolor?

will not work ok? you need r*65536+g*256+b*1
victerhh
Scratcher
9 posts

Detect Color by Number

its
touchingcolorr*65536+g*256+b?

its the only way

Powered by DjangoBB