May be you need this:
\def\parameter{\color{red}Y\futurelet\next\parameterA}
\def\parameterA{\ifx\next_\expandafter\parameterB\else\color{black}\fi}
\def\parameterB_#1{_{\color{black}#1}\color{black}}
$Y_i, \parameter_i, \parameter$ etc.
Edit: The code above was my first attempt to solve this problem. But two thinks were not treated here. First: color stack (the outer color may be blue, for example) and second: exponent (you can write \parameter_i^2 for example).
This is a reason why I suggest a second version of my macros:
\def\parameter{\bgroup\colorlet{outcolor}{.}\color{red}Y\futurelet\next\parameterA}
\def\parameterA{\ifx\next_\expandafter\parameterB\else\egroup\fi}
\def\parameterB_#1{_{{\color{outcolor}#1}}\futurelet\next\parameterC}
\def\parameterC{\ifx\next^\expandafter\parameterD\else\egroup\fi}
\def\parameterD^#1{^{\color{outcolor}#1}\egroup}
The explanation of the problem:
The index is positioned without italic correction of base character but exponent with italic correction. The previous sentence is not true if the last object of the base isn't character, i.e it is \pdfliteral or \special in order to return to the outer color. Then the italic correction is applied before such \pdfliteral. The following index (if exists) cannot remove the space from this italic correction after that. The result is: we cannot close the color in the base. We need to return to the outer color in the index manually.
Second problem: we cannot close the group (or to place \pdfliteral) after the index because the exponent (if exists) is positioned after such closing group and it is too far from the base. This is a reason why the exponent is treated similar way in may macros and the \egroup is set after exponent. Watch to the last trick: the double braces {{..}} in the index. The color setting in the xcolor LaTeX package is implemented as TeX-groups dependend by \aftergroup primitive. This places some material after the group where \color command is used. We need to save this material inside the index, no after the index is closed.
\mathcoloras defined in Colored symbols. – Peter Grill Jun 11 '14 at 05:56\protectis needed when using the solutions below in a caption. That link, however, uses it within the macro itself, so it is good to know that that is safe to do. The\leavevmodeisn't used in any of these solutions, so I wonder if it resolves an unseen problem (a quick search suggests spacing at beginning of paragraph, but that hasn't seemed to be an issue)? – Joel Bosveld Jun 11 '14 at 06:27