Example: search & replace a color
This example scans all shapes (current slide or all slides) for a source color and replaces it with a target color — with an adjustable tolerance and a selectable color aspect (fill / line / text). Typical use: roll an old brand color over to the new one everywhere.
The enum values (Füllung, Linie, Text, Alle) are language-neutral tokens read by the script.
Variant A — convenience helper ppptools.ReplaceColor
ppptools.ReplaceColor(shape, from, to, tolerance, aspect) does the comparison and replacement per shape:
// @param color SearchColor "Source color" default="#FFFFFF" tooltip="This color is searched"
// @param color ReplaceColor "Target color" default="#0070C0" tooltip="Replaces the source color"
// @param int Tolerance "Tolerance (0-255)" default="0" min="0" max="255" tooltip="0 = exact; higher = similar shades"
// @param enum Aspect "Color aspect" options="Füllung|Linie|Text|Alle" default="Füllung"
// @param bool AllSlides "All slides?" default="false"
// @button Replace
var from = Params.GetColor("SearchColor");
var to = Params.GetColor("ReplaceColor");
int tol = Params.GetInt("Tolerance", 0);
string asp = Params.GetString("Aspect", "Füllung");
void Process(System.Collections.Generic.List<dynamic> shapes)
{
foreach (dynamic sh in shapes)
ppptools.ReplaceColor(sh, from, to, tol, asp);
}
if (Params.GetBool("AllSlides", false))
for (int s = 1; s <= ppptools.SlideCount; s++)
Process(ppptools.GetAllShapes(ppptools.GetSlide(s)));
else
Process(ppptools.GetAllShapes());
Variant B — without the helper (full control)
If you want to control the comparison yourself (or use an older build without ReplaceColor), go straight through the COM API:
// @param color SearchColor "Source color" default="#FFFFFF"
// @param color ReplaceColor "Target color" default="#0070C0"
// @param int Tolerance "Tolerance (0-255)" default="0" min="0" max="255"
// @param enum Aspect "Color aspect" options="Füllung|Linie|Text|Alle" default="Füllung"
// @param bool AllSlides "All slides?" default="false"
// @button Replace
var from = Params.GetColor("SearchColor");
var to = Params.GetColor("ReplaceColor");
int tol = Params.GetInt("Tolerance", 0);
string asp = Params.GetString("Aspect", "Füllung");
int toOle = System.Drawing.ColorTranslator.ToOle(to);
bool Match(int ole)
{
var c = System.Drawing.ColorTranslator.FromOle(ole);
return System.Math.Abs(c.R - from.R) <= tol
&& System.Math.Abs(c.G - from.G) <= tol
&& System.Math.Abs(c.B - from.B) <= tol;
}
void Process(System.Collections.Generic.List<dynamic> shapes)
{
foreach (dynamic sh in shapes)
{
try
{
if ((asp == "Füllung" || asp == "Alle")
&& (int)sh.Fill.Visible == (int)MsoTriState.msoTrue
&& Match((int)sh.Fill.ForeColor.RGB))
sh.Fill.ForeColor.RGB = toOle;
if ((asp == "Linie" || asp == "Alle")
&& (int)sh.Line.Visible == (int)MsoTriState.msoTrue
&& Match((int)sh.Line.ForeColor.RGB))
sh.Line.ForeColor.RGB = toOle;
if ((asp == "Text" || asp == "Alle")
&& (int)sh.HasTextFrame == (int)MsoTriState.msoTrue
&& (int)sh.TextFrame.HasText == (int)MsoTriState.msoTrue
&& Match((int)sh.TextFrame.TextRange.Font.Color.RGB))
sh.TextFrame.TextRange.Font.Color.RGB = toOle;
}
catch { /* skip shapes without fill/line */ }
}
}
if (Params.GetBool("AllSlides", false))
for (int s = 1; s <= ppptools.SlideCount; s++)
Process(ppptools.GetAllShapes(ppptools.GetSlide(s)));
else
Process(ppptools.GetAllShapes());
Tolerance & groups
Tolerance 0 means an exact match; higher values catch slightly different shades (e.g. minimally shifted brand colors). Shapes inside groups are not traversed recursively — iterate GroupItems if needed.