ios - Finding left edge colour of UIImage -
ios - Finding left edge colour of UIImage -
i'm trying set uiview
background colour colour of uiimage's left border colour. think set background colour on uiview
match images background pretty closely.
i have tried average colour , doesn't work - colour of uiview
few shades off or wrong shade background colour of image. figured looking @ left border of uiimage
should give reasonably accurate representation of uiimage
's background colour.
doing searching , looking through questions - found bit of code, need. code os x , not ios.
- (nscolor*)findedgecolor:(nsimage*)image imagecolors:(nscountedset**)colors { nsbitmapimagerep *imagerep = [[image representations] lastobject]; if ( ![imagerep iskindofclass:[nsbitmapimagerep class]] ) // sanity check homecoming nil; nsinteger pixelswide = [imagerep pixelswide]; nsinteger pixelshigh = [imagerep pixelshigh]; nscountedset *imagecolors = [[nscountedset alloc] initwithcapacity:pixelswide * pixelshigh]; nscountedset *leftedgecolors = [[nscountedset alloc] initwithcapacity:pixelshigh]; ( nsuinteger x = 0; x < pixelswide; x++ ) { ( nsuinteger y = 0; y < pixelshigh; y++ ) { nscolor *color = [imagerep coloratx:x y:y]; if ( x == 0 ) { [leftedgecolors addobject:color]; } [imagecolors addobject:color]; } } *colors = imagecolors; nsenumerator *enumerator = [leftedgecolors objectenumerator]; nscolor *curcolor = nil; nsmutablearray *sortedcolors = [nsmutablearray arraywithcapacity:[leftedgecolors count]]; while ( (curcolor = [enumerator nextobject]) != nil ) { nsuinteger colorcount = [leftedgecolors countforobject:curcolor]; nsinteger randomcolorsthreshold = (nsinteger)(pixelshigh * kcolorthresholdminimumpercentage); if ( colorcount <= randomcolorsthreshold ) // prevent using random colors, threshold based on input image height continue; pccountedcolor *container = [[pccountedcolor alloc] initwithcolor:curcolor count:colorcount]; [sortedcolors addobject:container]; } [sortedcolors sortusingselector:@selector(compare:)]; pccountedcolor *proposededgecolor = nil; if ( [sortedcolors count] > 0 ) { proposededgecolor = [sortedcolors objectatindex:0]; if ( [proposededgecolor.color pc_isblackorwhite] ) // want take color on black/white maintain looking { ( nsinteger = 1; < [sortedcolors count]; i++ ) { pccountedcolor *nextproposedcolor = [sortedcolors objectatindex:i]; if (((double)nextproposedcolor.count / (double)proposededgecolor.count) > .3 ) // create sure sec selection color 30% mutual first selection { if ( ![nextproposedcolor.color pc_isblackorwhite] ) { proposededgecolor = nextproposedcolor; break; } } else { // reached color threshold less 40% of original proposed border color bail break; } } } } homecoming proposededgecolor.color; }
does know how can same thing ios? appreciate help!
as done in https://github.com/lukaswelte/colorart
you can via:
- (uicolor*)_findedgecolor:(uiimage*)image imagecolors:(nsarray**)colors { cgimageref imagerep = image.cgimage; nsuinteger pixelrange = 32; nsuinteger scale = 256 / pixelrange; nsuinteger rawimagecolors[pixelrange][pixelrange][pixelrange]; nsuinteger rawedgecolors[pixelrange][pixelrange][pixelrange]; // should switch calloc, doesn't show in instruments // guess it's fine for(nsuinteger b = 0; b < pixelrange; b++) { for(nsuinteger g = 0; g < pixelrange; g++) { for(nsuinteger r = 0; r < pixelrange; r++) { rawimagecolors[r][g][b] = 0; rawedgecolors[r][g][b] = 0; } } } nsinteger width = cgimagegetwidth(imagerep);// [imagerep pixelswide]; nsinteger height = cgimagegetheight(imagerep); //[imagerep pixelshigh]; cgcolorspaceref cs = cgcolorspacecreatedevicergb(); cgcontextref bmcontext = cgbitmapcontextcreate(null, width, height, 8, 4 * width, cs, kcgimagealphanoneskiplast); cgcontextdrawimage(bmcontext, (cgrect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = height}, image.cgimage); cgcolorspacerelease(cs); const rgbapixel* pixels = (const rgbapixel*)cgbitmapcontextgetdata(bmcontext); (nsuinteger y = 0; y < height; y++) { (nsuinteger x = 0; x < width; x++) { const nsuinteger index = x + y * width; rgbapixel pixel = pixels[index]; byte r = pixel.red / scale; byte g = pixel.green / scale; byte b = pixel.blue / scale; rawimagecolors[r][g][b] = rawimagecolors[r][g][b] + 1; if(0 == x) { rawedgecolors[r][g][b] = rawedgecolors[r][g][b] + 1; } } } cgcontextrelease(bmcontext); nsmutablearray* imagecolors = [nsmutablearray array]; nsmutablearray* edgecolors = [nsmutablearray array]; for(nsuinteger b = 0; b < pixelrange; b++) { for(nsuinteger g = 0; g < pixelrange; g++) { for(nsuinteger r = 0; r < pixelrange; r++) { nsuinteger count = rawimagecolors[r][g][b]; if(count > _randomcolorthreshold) { uicolor* color = [uicolor colorwithred:r / (cgfloat)pixelrange green:g / (cgfloat)pixelrange blue:b / (cgfloat)pixelrange alpha:1]; pccountedcolor* countedcolor = [[pccountedcolor alloc] initwithcolor:color count:count]; [imagecolors addobject:countedcolor]; } count = rawedgecolors[r][g][b]; if(count > _randomcolorthreshold) { uicolor* color = [uicolor colorwithred:r / (cgfloat)pixelrange green:g / (cgfloat)pixelrange blue:b / (cgfloat)pixelrange alpha:1]; pccountedcolor* countedcolor = [[pccountedcolor alloc] initwithcolor:color count:count]; [edgecolors addobject:countedcolor]; } } } } *colors = imagecolors; nsmutablearray* sortedcolors = edgecolors; [sortedcolors sortusingselector:@selector(compare:)]; pccountedcolor *proposededgecolor = nil; if ( [sortedcolors count] > 0 ) { proposededgecolor = [sortedcolors objectatindex:0]; if ( [proposededgecolor.color pc_isblackorwhite] ) // want take color on black/white maintain looking { ( nsinteger = 1; < [sortedcolors count]; i++ ) { pccountedcolor *nextproposedcolor = [sortedcolors objectatindex:i]; if (((double)nextproposedcolor.count / (double)proposededgecolor.count) > .4 ) // create sure sec selection color 40% mutual first selection { if ( ![nextproposedcolor.color pc_isblackorwhite] ) { proposededgecolor = nextproposedcolor; break; } } else { // reached color threshold less 40% of original proposed border color bail break; } } } } homecoming proposededgecolor.color; }
if want left edge, can accomplish excluding loop other edges.
ios objective-c osx uiview uiimage
Comments
Post a Comment