grep is a GNU program another option might be having a look at the source code.
Internally grep tests the
static int color_option for either
0 never use colorized output
1 always use colors
2 only use colors when printing to a terminal
Now when you hand over
--color=auto to grep as an argument on your CLI, it internally sets the variable
grep then further tests whether
STDOUT is linked to a terminal or the user disabled colorized outpit via
shell environment variables.
If the former one is true and colorized output is permitted,
grep then continues with evaluating which colors should be used and in the end finally prints out to your
CLI in color.
source: (grep 2.21)
grep.c line 306, 2374, 2440
colorize-posix.c line 36