Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do I get the rough color of what the sprite is touching?
- ham07
-
Scratcher
100+ posts
How do I get the rough color of what the sprite is touching?
when green flag clicked
set [R v] to [0]
set [G v] to [0]
set [B v] to [0]
repeat until <<touching color (((((R) * (65536))) + ((G) * (256))) + (B)) ?> or <(B) > [254]>>
repeat until <<touching color (((((R) * (65536))) + ((G) * (256))) + (B)) ?> or <(G) > [254]>>
repeat until <<touching color (((((R) * (65536))) + ((G) * (256))) + (B)) ?> or <(R) > [254]>>
change [R v] by (1)
end
set [R v] to [0]
change [G v] by (1)
end
set [G v] to [0]
change [B v] by (1)
end
set [Color v] to [(((((R) * (65536))) + ((G) * (256))) + (B))]
doesn't work
(btw I took about 1 - 5 mins doing the scratch blocks ^^^)
- ham07
-
Scratcher
100+ posts
How do I get the rough color of what the sprite is touching?
I mean:
set [Color v] to (((((R) * (65536))) + ((G) * (256))) + (B))
- deck26
-
Scratcher
1000+ posts
How do I get the rough color of what the sprite is touching?
Well it wouldn't. If an inner repeat until loop detects the colour the outer loop will carry on - it will change its value before testing its own repeat until condition.
So if B and G are set correctly and you stop the R loop because you've found a match you then set R toi0 and change G so the G loop will not see any reason to stop.
Try using a simple flag variable. Set it to 0 and your loops repeat until the variable is 1. When you change one of the RGB values you check for touching and if touching set the variable to 1 else change whichever RGB variable you're working on.
So if B and G are set correctly and you stop the R loop because you've found a match you then set R toi0 and change G so the G loop will not see any reason to stop.
Try using a simple flag variable. Set it to 0 and your loops repeat until the variable is 1. When you change one of the RGB values you check for touching and if touching set the variable to 1 else change whichever RGB variable you're working on.
- ham07
-
Scratcher
100+ posts
How do I get the rough color of what the sprite is touching?
can you try to give an example in
please, so I can get a better understanding
- deck26
-
Scratcher
1000+ posts
How do I get the rough color of what the sprite is touching?
can you try to give an example in scratchblocks please, so I can get a better understandingThink your code through - what happens when the inner loop detects the correct colour? The first thing you do i set R to 0 throwing away the information you've just worked out. You then change G so you've lost that value as well!
This isn't difficult. In pseudo code
set flag to 0
set b to 0
repeat until flag = 1 or b > 254 (blue)
set g to 0
repeat until flag = 1 or g > 254 (green)
set r to 0
repeat until flag = 1 or r > 254
if touching colour rgb set flag to 1
else change r by 8
end r loop
if flag=0 change g by 8
end g loop
if flag = 0 change b by 16
end b loop
rgb have correct values here unless flag is still 0
However please note the following. The colour checker appears to only check the first 5 bits of the red & green components, and the first 4 bits of the blue component. This means the total checks needed to find a match is 32*32*16, rather than the expected 256*256*256 (which is >16 million). That's why the rgb values above are changed by 8/8/16.
See this old topic https://scratch.mit.edu/discuss/topic/64772/
Last edited by deck26 (July 23, 2020 15:39:06)
- cs2925723
-
Scratcher
100+ posts
How do I get the rough color of what the sprite is touching?
Here's deck26's code in scratchblocks:
...::hat
set [flag v] to [0]
set [b v] to [0]
repeat until <<(flag) = [1]> or <(b::#0029FE) > (254)>>
set [g v] to [0]
repeat until <<(flag) = [1]> or <(g::#24FE00) > (254)>>
set [r v] to [0]
repeat until <<(flag) = [1]> or <(r::#FE0000) > (254)
if <touching color (join (r) (join [/] (join (g) (join [/] (b)))))> then
set [flag v] to [1]
else
change [r v] by (8)
end
end
if <(flag) = [0]> then
change [g v] by (8)
end
end
if <(flag) = [0]> then
change [b v] by (8)
end
end
- NOTE: The color of the b, g, and r variables is only to show what color they represent. The real variable will be orange.
Last edited by cs2925723 (July 23, 2020 17:34:31)
- Cutie_Pooge
-
Scratcher
500+ posts
How do I get the rough color of what the sprite is touching?
You don't join them, here.
- Discussion Forums
- » Help with Scripts
-
» How do I get the rough color of what the sprite is touching?
