Wish [B4X] ColorPicker only a line like in whatsapp

Alexander Stolte

Licensed User
the color pickers that exist are all well and good, but i don't want to open a dialog to let someone choose a color.
Any plans to add so a view? Would certainly be a good addition to XUI views.



Active Member
Licensed User
I did this long ago. It's hard to clean those extra codes but it's easy to build.
  1. Generate a color palette/download a png image.
  2. Set that to the panel's background.
  3. On touch event get the color of the point.
    1. dim that panel as b4xview
    2. get the snapshot
    3. get the pixel color and store it in a variable.
In B4A/B4J you can use GetPixel method of bitmap.
In B4I you can use this following code to get the pixel color.
Dim no as NativeObject
Dim color as Int = no.UIColorToColor(no.RunMethod("GetPixelColor:::",Array(bitmap,X, Y)))

#if OBJC
- (UIColor *)GetPixelColor:(UIImage*)image :(int)xp :(int)yp {
    NSMutableArray *resultColor = [NSMutableArray array];
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * yp) + xp * bytesPerPixel;
    CGFloat red   = (rawData[byteIndex]     * 1.0) /255.0;
    CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0 ;
    CGFloat blue  = (rawData[byteIndex + 2] * 1.0)/255.0 ;
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0) /255.0;
    byteIndex += 4;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];


    return color;
#End If
Last edited: