Color () is Touching ()?
2.0 Color () is Touching Color ().png
Category Sensing
Type Boolean

The Color () is Touching ()? block is a Sensing block and a Boolean block. The block checks whether a color on its sprite is touching another color. If it is, the block returns "true".

This block is widely used for collision detection.

Arguments

The color to be detected can be entered by either:

  1. clicking on one of the block's color swatches, then moving the mouse pointer to the desired color, and clicking. As the mouse is moved, the color swatch continuously updates to reflect the color under the mouse pointer. When the desired color is located, a click will set the color swatch. (Note that the swatch will cease to react if the pointer strays beyond the edges of the Scratch project.) For example, if the mouse pointer is over the color blue when the mouse is clicked, the color swatch will turn blue.
  2. placing Operators blocks into the color swatches, in the following format:
<color (((r) * (65536)) + (((g) * (256)) + (b))) is touching (((r) * (65536)) + (((g) * (256)) + (b))) ?>
where "r", "g", and "b" are the standard values for Red, Green, and Blue, respectively.

History

The block as seen in Scratch 1.2

In versions of Scratch before 1.2, this block was called Color () is Over ()?.


Caveats

The Color () is Touching ()? block can produce unexpected/unwanted results.

Built-in Limitation

Significantly, the Color () is Touching ()? block does not always return "false" if its sprite color is not touching the other specified color. This is because color sensing in Scratch has a built-in limitation, which reduces processing time.[citation needed] Whilst the stage can display more than 16 million colors, Scratch only properly deals with a much smaller number of colors.[1] This means that the Color () is Touching ()? block will often return a "false positive" when sensing a color which is different from (but similar to) the specified one. Many Scratchers may never notice this phenomenon, but those wanting absolute precision in color detection should bear the Scratch limitation in mind.

Anti-Aliasing

Some graphics, even if they appear to have crisp edges in the Scratch Paint Editor, are subject to anti-aliasing when on the stage. This means that, when selecting color by clicking, care must be taken not to select a translucent edge pixel by mistake.

Performance

Scratchers considering using the Color () is Touching ()? block should be aware that it performs slower than the Touching ()? (block).[2] Its performance improves if placed in a () Custom block set to run without screen refresh, but in the same Custom Block, a Touching ()? (block) will out-perform it by an even greater margin.

Example Uses

  • Moving a sprite until a color on its sprite touches a color.
repeat until <color [#ea235c] is touching [#cba81a]?>
point towards [Sprite2 v]
move (10) steps
end
  • Making a sprite do something if a color on its sprite touches a color, e.g., if the sprite's (white) mouth touches (blue) water, it drinks some water.
forever
if <color [#ffffff] is touching [#0000ff]?> then
switch to costume [drinking water v] :: looks
wait (1) secs
switch to costume [not drinking water v] :: looks 
  • Stopping bullets if they hit walls of a specific color.
repeat until <color [#ffd700] is touching [#f321a5]?>
move (10) steps
end
delete this clone
  • Sensing whether a sprite has hit a dead-end in a maze.
forever
if <color [#000000] is touching [#007f00]?> then
say [Uh-oh, I hit a dead end!] for (2) secs

See Also

References

  1. https://scratch.mit.edu/projects/10941245/
  2. https://scratch.mit.edu/projects/139167694/