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 value.
This can be a string in the following format "HSL h s l a", "YUV y u v a", "RGB r g b a", "MONO m a", "GRAY g a" or "COLOR R G B a". Alpha value a is optional. Range of values is 0 to 1.0 except if you use COLOR where RGB values are 0 to 65535.
Starting with version 5.4 of our plugin, you can use hex notation. # followed by 2 digits for red, green, blue and optional alpha, e.g. #FF0000 for full red.
"RGB 1 0 0"
color2 A color value.
This can be a string in the following format "HSL h s l a", "YUV y u v a", "RGB r g b a", "MONO m a", "GRAY g a" or "COLOR R G B a". Alpha value a is optional. Range of values is 0 to 1.0 except if you use COLOR where RGB values are 0 to 65535.
Starting with version 5.4 of our plugin, you can use hex notation. # followed by 2 digits for red, green, blue and optional alpha, e.g. #FF0000 for full red.
"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; Wert:MBS( "GMImage.NewFromContainer"; Replace Colors::Input)]
Set Variable [$r; Wert:MBS( "GMImage.ReplaceColor"; $img; "COLOR 65278 65278 65278"; "COLOR 65535 0 0")]
Set Variable [Replace Colors::Output; MBS( "GMImage.WriteToPNGContainer"; $img; "logo.png" )]
Set Variable [$r; Wert: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 25th November 2020


GMImage.Repage - GMImage.Resize