iOS Question replace a colour in bitmap image

Starchild

Active Member
Licensed User
Longtime User
I want to replace a spot colour in an icon's bitmap with a different colour.
I need to do this pragmatically.

In B4A I was using the "ReplaceColor" function in the library "ImageProcessing".

Can someone please help.
 

Starchild

Active Member
Licensed User
Longtime User
Are you trying to replace a specific color value? Can you upload an example image?

Yes. the bitmaps are 128x128 icons containing just a few spot colors. I need to make one of the colours transparent. I assume I need to make a mask.

I found this at:
https://stackoverflow.com/questions/8311049/ios-color-to-transparent-in-uiimage
B4X:
+ (UIImage*) processImage :(UIImage*) image
{
    const float colorMasking[6]={222,255,222,255,222,255};
    CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
    UIImage* imageB = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return imageB;
}

It makes the colour WHITE transparent in the UIImage.
I don't know how to use this in B4i.
I also need it to work with the B4i BITMAP object.
I would appreciate any help converting or an alternate solution.
 
Last edited:
Upvote 0

Starchild

Active Member
Licensed User
Longtime User
If anyone else may need to replace a colour in a bitmap with transparent this is the final code I ended up using. My solution creates a mask for the source bitmap.

ObjC code from this Ref:
https://stackoverflow.com/questions/19443311/how-to-make-one-colour-transparent-in-uiimage

B4X:
'Returns the bitmap with the selected colour
'replaced as transparent.
'Tolerance = 0 to 100 percent
Public Sub Transparent(bmp As Bitmap,TranparentColour As Int, Tolerance As Int) As Bitmap
 
    Dim Red, RedLow, RedHigh As Int
    Dim Green, GreenLow, GreenHigh As Int
    Dim Blue, BlueLow, BlueHigh As Int
 
    Red = GetByte(TranparentColour,2)
    Green = GetByte(TranparentColour,1)
    Blue = GetByte(TranparentColour,0)
 
    Tolerance = (Tolerance * 255) / 100
 
    RedLow = Red - Tolerance
    If RedLow < 0 Then RedLow = 0
    GreenLow = Green - Tolerance
    If GreenLow < 0 Then GreenLow = 0
    BlueLow = Blue - Tolerance
    If BlueLow < 0 Then BlueLow = 0
 
    RedHigh = Red + Tolerance
    If RedHigh > 255 Then RedHigh = 255
    GreenHigh = Green + Tolerance
    If GreenHigh > 255 Then GreenHigh = 255
    BlueHigh = Blue + Tolerance
    If BlueHigh > 255 Then BlueHigh = 255
 
    Dim no As NativeObject = Me
    Dim B As Bitmap = no.RunMethod("changeColorTransparent:::::::", Array(bmp,RedLow,RedHigh,GreenLow,GreenHigh,BlueLow,BlueHigh))
    Return B
End Sub
 
#If objc
-(UIImage *) changeColorTransparent : (UIImage *)image : (int)rl : (int)rh : (int)gl : (int)gh : (int)bl : (int)bh
{
   CGImageRef rawImageRef = image.CGImage;
   CGFloat colorMasking[6] = {rl, rh, gl, gh, bl, bh};
   UIGraphicsBeginImageContext(image.size);
   CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
    {
        //if in iPhone
   CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
   CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    }
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
    UIGraphicsEndImageContext();
    return result;
}
#End If
'returns one byte from "Integer" only
'(where ByteNumber = 0 to 3)
Private Sub GetByte(Integer As Int,ByteNumber As Int) As Int
     Dim V As Long = Integer
    Dim A As Long = Power(256,4)
    If V < 0 Then
        V = A + (V)
    End If
 
     If (ByteNumber < 0) Or (ByteNumber > 3) Then
        Return -1  'invalid byte number
    End If
 
     Do Until ByteNumber = 0
        V = V / 256
        ByteNumber = ByteNumber - 1
    Loop
 
    Dim I As Long = V
    Return I Mod 256
End Sub

Note:
To do a replacement of one colour for another in a bitmap, then Erels reference above would need to be employed. That ref converts the bitmap to/from a byte array.
 
Last edited:
Upvote 0
Top