Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

GMImage.ReplaceColor

Replaces a color with another color value for the whole image.

Component Version macOS Windows Linux Server iOS SDK
GraphicsMagick 4.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "GMImage.ReplaceColor"; ImageRef; color1; color2 { ; Tolerance } )   More

Parameters

Parameter Description Example Flags
ImageRef The image reference number. 1
color1 A color specification.
Multiple formats are supported including various color names and # with hex number.
See FAQ for details.
"RGB 1 0 0"
color2 A color specification.
Multiple formats are supported including various color names and # with hex number.
See FAQ for details.
"RGB 0 1 0"
Tolerance The tolerance to allow for a match.
Pass 1 to have one channel be matched if value is one away, e.g. 128 is accepted if searched for 127 or 129. Default is 0.

Since MBS Plugin 9.x uses 16-bit values, please multiple by 257 to expand older 8-bit values to 16-bit.
5 * 257 Optional

Result

Returns OK or error.

Description

Replaces a color with another color value for the whole image.
Match is exact, so no fuzzy matching unless you pass tolerance parameter.

Examples

Replaces a nearly white color with red:

Set Variable [$img; Value: MBS( "GMImage.NewFromContainer"; Replace Colors::Input)]
Set Variable [$r; Value: MBS( "GMImage.ReplaceColor"; $img; "COLOR 65278 65278 65278"; "COLOR 65535 0 0")]
Set Field [Replace Colors::Output; Value: MBS( "GMImage.WriteToPNGContainer"; $img; "logo.png" )]
Set Variable [$r; Value: MBS( "GMImage.Release"; $img )]

Replace white with transparent:

Set Variable [$BigImageRef; Value:MBS("GMImage.NewFromContainer"; Test::BigImage)]
# Set type to RGBA
Set Variable [$Result; Value:MBS("GMImage.SetType"; $BigImageRef; 7)]
# Replace white with transparent
Set Variable [$Result; Value:MBS("GMImage.ReplaceColor"; $BigImageRef; "white"; "transparent"; 10 * 257)]
# save as PNG
Set Field [Test::Result; MBS( "GMImage.WriteToPNGContainer"; $BigImageRef )]
Set Variable [$Error; Value:MBS("GMImage.Release";$BigImageRef)]

Trims space around image and make transparent:

Set Variable [$BigImageRef; Value:MBS("GMImage.NewFromContainer"; Test::BigImage)]
Set Variable [$Result; Value:MBS( "GMImage.SetBackgroundColor"; $BigImageRef; "RGB 1 0 0" )]
Set Variable [$Result; Value:MBS("GMImage.Trim"; $BigImageRef)]
Set Variable [$Result; Value:MBS( "GMImage.SetColorFuzz"; $BigImageRef; 5 * 257 )]
Set Variable [$Result; Value:MBS( "GMImage.SetMatte"; $BigImageRef; 1 )]
Set Variable [$farbe; Value:MBS( "GMImage.GetPixelColor"; $BigImageRef; 0; 0 )]
Set Variable [$Result; Value:MBS( "GMImage.ReplaceColor"; $BigImageRef; $farbe; "RGB 1 1 1 1"; 5 * 257)]
Set Field [Test::Result; MBS( "GMImage.WriteToPNGContainer"; $BigImageRef )]
Set Variable [$Error; Value:MBS("GMImage.Release";$BigImageRef)]

Replace black in black & white picture with red:

Set Variable [ $image ; Value: MBS( "GMImage.NewFromContainer"; test::Input ) ]
Set Variable [ $r ; Value: MBS( "GMImage.ReplaceColor"; $image; "black"; "red"; 1 * 257) ]
Set Field [ test::Output ; MBS( "GMImage.WriteToPNGContainer"; $image) ]
Set Variable [ $r ; Value: MBS( "GMImage.Release"; $image) ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 6th September 2014, last changed 30th June 2026


GMImage.Repage - GMImage.Resize


MarkDown version: GMImageReplaceColor.md