Try this on for size. One warning: do not just cut and paste this code, as it will probably not work the way you expect it to work (if it works at all). Please try to study it before using it.
# Assign your color here.
var le_collider = some_raycast.get_collider()
# Check if "le_collider" is of the same class as the grass (or dirt, or metal, or rock, or...) block. For this example, we'll assume it's a TileMap.
if le_collider is TileMap:
# Now check to see if the TileMap is in the "Ground" group (or whatever group you want to place these blocks in).
# Check for which block is being used. How do you do this? That's up to you. Here, I'll match names. Side note: isn't this starting to look like an "if" arrow?
color = "green"
color = "brown"
color = "silver"
# Couldn't figure out the color. Print an error and exit.
push_error("Couldn't determine block color.")
# Now assign the color. How you do that is up to you. The following function is made up.