GMImage.ReplaceColor
--------------------

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

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [GraphicsMagick](component_GraphicsMagick.md) | [4.3](newinversion43.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "GMImage.ReplaceColor"; ImageRef; color1; color2 { ; Tolerance } ) 

**MBS**( **"GMImage.ReplaceColor";** /\* Replaces a color with another color value for the whole image. \*/  
**$ImageRef**; /\* The image reference number.e.g. 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.e.g. "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.e.g. "RGB 0 1 0" \*/   
**$Tolerance**) /\* Optional; 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.e.g. 5 \* 257 \*/ 

### 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 ; Value: MBS ( "[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; 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](GMImageWriteToPNGContainer.md)"; $img ; "logo.png" )\]  
Set Variable \[$r ; Value: MBS ( "[GMImage.Release](GMImageRelease.md)"; $img )\]  
Replace white with transparent:

 Set Variable \[$BigImageRef ; Value:MBS ("[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; Test::BigImage )\]  
\# Set type to RGBA   
Set Variable \[$Result ; Value:MBS ("[GMImage.SetType](GMImageSetType.md)"; $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](GMImageWriteToPNGContainer.md)"; $BigImageRef )\]  
Set Variable \[$Error ; Value:MBS ("[GMImage.Release](GMImageRelease.md)";$BigImageRef )\]  
Trims space around image and make transparent:

 Set Variable \[$BigImageRef ; Value:MBS ("[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; Test::BigImage )\]  
Set Variable \[$Result ; Value:MBS ( "[GMImage.SetBackgroundColor](GMImageSetBackgroundColor.md)"; $BigImageRef ; "RGB 1 0 0" )\]  
Set Variable \[$Result ; Value:MBS ("[GMImage.Trim](GMImageTrim.md)"; $BigImageRef )\]  
Set Variable \[$Result ; Value:MBS ( "[GMImage.SetColorFuzz](GMImageSetColorFuzz.md)"; $BigImageRef ; 5 \* 257 )\]  
Set Variable \[$Result ; Value:MBS ( "[GMImage.SetMatte](GMImageSetMatte.md)"; $BigImageRef ; 1 )\]  
Set Variable \[$farbe ; Value:MBS ( "[GMImage.GetPixelColor](GMImageGetPixelColor.md)"; $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](GMImageWriteToPNGContainer.md)"; $BigImageRef )\]  
Set Variable \[$Error ; Value:MBS ("[GMImage.Release](GMImageRelease.md)";$BigImageRef )\]  
Replace black in black &amp; white picture with red:

 Set Variable \[ $image ; Value: MBS ( "[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; test::Input ) \]   
Set Variable \[ $r ; Value: MBS ( "GMImage.ReplaceColor"; $image ; "black"; "red"; 1 \* 257) \]   
Set Field \[ test::Output ; MBS ( "[GMImage.WriteToPNGContainer](GMImageWriteToPNGContainer.md)"; $image ) \]   
Set Variable \[ $r ; Value: MBS ( "[GMImage.Release](GMImageRelease.md)"; $image ) \]  
### See also

- [Barcode.Generate](BarcodeGenerate.md)
- [GMImage.FloodFillColorXY](GMImageFloodFillColorXY.md)
- [GMImage.GetMatteColor](GMImageGetMatteColor.md)
- [GMImage.New](GMImageNew.md)
- [GMImage.NewFromContainer](GMImageNewFromContainer.md)
- [GMImage.Release](GMImageRelease.md)
- [GMImage.SetBackgroundColor](GMImageSetBackgroundColor.md)
- [GMImage.SetColorFuzz](GMImageSetColorFuzz.md)
- [GMImage.SetMatte](GMImageSetMatte.md)
- [GMImage.Trim](GMImageTrim.md)

### Example Databases

- [GraphicsMagick/Trim Picture](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/GraphicsMagick/Trim%20Picture.shtml#4ScriptAnchor_)

### Blog Entries

- [GraphicsMagick in FileMaker, part 17](https://www.mbsplugins.de/archive/2022-12-17/GraphicsMagick_in_FileMaker_pa/monkeybreadsoftware_blog_filemaker)
- [MBS Filemaker Plugin, version 4.3pr10](https://www.mbsplugins.de/archive/2014-09-09/MBS_Filemaker_Plugin_version_4/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2018, Seite 27](https://filemaker-magazin.de/neuigkeit/4011-Appetithappen-FMM_201805)

This function checks for a license.

Created 6th September 2014 , last changed 24th February 2026

  
[GMImage.Repage](GMImageRepage.md) - [GMImage.Resize](GMImageResize.md)

[HTML Version](GMImageReplaceColor.shtml)