Document.png Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (January 2022)

Color detection is detecting the color a sprite is touching, primarily using lists and the Touching Color block.

Programming

Script

Only one variable is necessary for the following script, being (color).

The following custom block is the script for color detection:

define get color // run without screen refresh
set [color v] to (0)
repeat (16777215) // 256^3-1=16777215, the total amount of colors possible minus one
  if <touching color (color)?> then // if it finds the color, the script stops
    stop [this script v]
  end
  change [color v] by (1) // increments the color each time through the loop
end

Abusing Color Sensitivity

The previous script may be very slow depending on the color detected, but can be sped up by abusing color sensitivity. Scratch is unable to sense all 16777216 possible colors, and will only sense 16 different shades of blue and 32 different shades of red and green.

Improved Script

define get color // run without screen refresh
set [color v] to (0)
repeat (32)
  repeat (32)
    repeat (16)
    if <touching color (color)?> then
      stop [this script v]
      end
    change [color v] by (16) // cycles through shades of blue
    end
    change [color v] by (1792) // cycles through shades of green
  end
  change [color v] by (458752) // cycles through shades of red
end

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.