Beispiel: Farbe suchen & ersetzen
Dieses Beispiel durchsucht alle Shapes (aktuelle Folie oder alle Folien) nach einer Quellfarbe und ersetzt sie durch eine Zielfarbe — mit einstellbarer Toleranz und wählbarem Farbaspekt (Füllung / Linie / Text). Typischer Einsatz: eine alte Markenfarbe flächendeckend auf die neue umstellen.
Variante A — Komfort-Helfer ppptools.ReplaceColor
ppptools.ReplaceColor(shape, von, nach, toleranz, aspekt) erledigt Vergleich und Ersetzung pro Shape:
// @param color SearchColor "Suchfarbe" default="#FFFFFF" tooltip="Diese Farbe wird gesucht"
// @param color ReplaceColor "Zielfarbe" default="#0070C0" tooltip="Ersetzt die Suchfarbe"
// @param int Tolerance "Toleranz (0-255)" default="0" min="0" max="255" tooltip="0 = exakt; höher = ähnliche Töne"
// @param enum Aspect "Farbaspekt" options="Füllung|Linie|Text|Alle" default="Füllung"
// @param bool AllSlides "Alle Folien?" default="false"
// @button Ersetzen
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());
Variante B — ohne Helfer (volle Kontrolle)
Wenn du den Vergleich selbst steuern willst (oder eine ältere Version ohne ReplaceColor nutzt), geht es auch direkt über die COM-API:
// @param color SearchColor "Suchfarbe" default="#FFFFFF"
// @param color ReplaceColor "Zielfarbe" default="#0070C0"
// @param int Tolerance "Toleranz (0-255)" default="0" min="0" max="255"
// @param enum Aspect "Farbaspekt" options="Füllung|Linie|Text|Alle" default="Füllung"
// @param bool AllSlides "Alle Folien?" default="false"
// @button Ersetzen
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 { /* Shapes ohne Fill/Line überspringen */ }
}
}
if (Params.GetBool("AllSlides", false))
for (int s = 1; s <= ppptools.SlideCount; s++)
Process(ppptools.GetAllShapes(ppptools.GetSlide(s)));
else
Process(ppptools.GetAllShapes());
Toleranz & Gruppen
Toleranz 0 heisst exakte Übereinstimmung; höhere Werte fangen leicht abweichende Töne (z. B. minimal verschobene Markenfarben). Shapes innerhalb von Gruppen werden nicht rekursiv erfasst — bei Bedarf GroupItems durchlaufen.