UIColor always gives white when settings blue - uiimageview

I'm trying to set the background color of a UIImageView with this (It should return some green-ish color):
cell.background.backgroundColor = [UIColor colorWithRed:50 green:200 blue:50 alpha:1];
But for some reason this is only giving me a white color, it seems that whenever I set the blue it turns back to white, when I do this, it returns me yellow, which is normal:
cell.background.backgroundColor = [UIColor colorWithRed:50 green:200 blue:0 alpha:1];

Ok, I didn't realize UIColor requires a float from 0 - 1, not from 0 - 255, this works:
cell.background.backgroundColor = [UIColor colorWithRed:240.0f/255.0f green:245.0f/255.0f blue:254.0f/255.0f alpha:1];

Related

Displaying an image with alpha channel on OSX

I am trying to display a 32bit, 4 channeled image in my window. The fourth channel in the image is an alpha channel. As an experiment, I create a completely Red image, where each pixel has the RGB value: 255,0,0. For each pixel I also add an alpha value of 204.
What I expected to see was a completely Red image with some transparency, but what I see instead is a completely opaque image with the values altered.
Expected Output:
Current output:
The code I am using:
NSRect windowRect = {0,0,200,200};
m_NSWindow = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[m_NSWindow setTitle:#"overlayWindow"];
[m_NSWindow makeKeyAndOrderFront:nil];
g_imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,200,200)];
[m_NSWindow.contentView addSubview:g_imageView];
[m_NSWindow setOpaque:NO];
[m_NSWindow setAlphaValue:1.0];
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:200
pixelsHigh:200
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:(200*4)
bitsPerPixel:32];
memcpy(imageRep.bitmapData,m_paintBuffer.data,160000);
NSSize imageSize = NSMakeSize(200,200);
NSImage* myImage = [[NSImage alloc] initWithSize: imageSize];
[myImage addRepresentation:imageRep];
[g_imageView setImage:myImage];
where m_paintBuffer.data points to the raw pixel data for the image.
Not sure if it is relevant to the question, but m_paintBuffer is of type Mat from OpenCV
The window's content view is not the entirety of the window's content, as it were. Even a borderless window has (private) theme views surrounding and containing the content view.
In particular, the window has a background color of a light gray. This is drawn "behind" your content view and is opaque. However, you can change it by setting the window's backgroundColor property to, for example, [NSColor clearColor]. And that's what you should do to achieve your desired effect.

How do I change the alpha of an NSBezierpath filled with an image?

Note: this is on OSX NOT iOs...
NSColor has a neat helper method called "colorWithPatternImage" that allows you to treat any image as a "fill" on, for instance, an NSBezierPath.
I would like to use this to create multiple overlapping squares that contain images whose opacity I can change.
The trouble is the following:
// ok - solid white
NSColor *fillColor = [NSColor whiteColor];
// ok - same as above
fillColor = [[NSColor whiteColor] colorWithAlphaComponent:1.0];
// ok - alpha of 50% (light grey)
fillColor = [[NSColor whiteColor] colorWithAlphaComponent:0.5];
// ok - fills with content of NSImage backgroundImage
fillColor = [NSColor colorWithPatternImage:backgroundImage];
// ok - same as above
fillColor = [[NSColor colorWithPatternImage:backgroundImage] colorWithAlphaComponent:1.0];
// FAILS! - any value less than 1.0 acts like alpha is 0.0
fillColor = [[NSColor colorWithPatternImage:backgroundImage] colorWithAlphaComponent:0.9];
Any thoughts on how to accomplish this? I did try first re-drawing the NSImage with an alpha and then using it as a "patternImage." While that does properly apply the alpha to the image itself, it does nothing to the path, so you end up with a "dimmed" but opaque image.
What am I missing?

Custom Font color (Dark Navy Blue)

I'm trying to make a font the same color as a detail disclosure button (dark purple or dark navy blue).
I used the following combination [UIColor colorWithRed: 187.0/255.0f green:107.0/255.0f blue:207.0/255.0f alpha:1.0] but it is not even close. Can anyone please provide me with the right combination of red green and blue.
For color of the background of a detail disclosure button you'll want something like:
[UIColor colorWithRed: 28./255.f green:86./255.f blue:207./255.f alpha:1.0]
For the font color of a standard UIButton:
[UIColor colorWithRed: 38./255.f green:60./255.f blue:113./255.f alpha:1.0]
Something like this: [UIColor colorWithRed: 0f green:0f blue:0.5f alpha:1.0] should be closer.
If you want other colors, use a color chart like this one on Wikipedia, and just convert the percentages to values between 0 and 1 (e.g. 30% = 0.3)

CALayer thin white lines

I am animating a CALayer to move and for some reason I keep getting these thin white lines all across the screen... it's not my device I can assure you here is how I set up the CALayer:
dodgelayer=nil;
dodgelayer= [CALayer layer];
dodgelayer.backgroundColor = [UIColor blueColor].CGColor;
dodgelayer.frame = CGRectMake(190, 80, 50, 50);
dodgelayer.borderColor = [UIColor whiteColor].CGColor;
dodgelayer.borderWidth = 2.0;
dodgelayer.cornerRadius = 50.0;
and how I animate it:
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint: CGPointMake(aLayer.frame.origin.x+25,aLayer.frame.origin.y+25)], [NSValue valueWithCGPoint: CGPointMake(point.x,point.y)], nil];
[anim setValues:values];
[anim setDuration:0.7];
anim.removedOnCompletion=NO;
anim.fillMode = kCAFillModeForwards;
anim.timingFunction=[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
sublayer.shouldRasterize=YES;
[sublayer addAnimation:anim forKey:#"position"];
I honestly have no idea what is making these thing white lines appear, but I can tell you that they appear during animations... im stumped on this one any help would be appreciated
I just bumped into this problem myself.
The cause is the shouldResterize = YES.
Before calling this, you should set
sublayer.rasterizationScale = [[UIScreen mainScreen] scale];
and the lines will go away.

Drawing color content of NSColorWell to an NSTextView

Thanks again for the help -
What's the best way to insert a 60x60 pixel square representation of a color displayed in a colorWell into a textView? I know how to reference the values of the displayed color.
Again. thanks.
-paul.
This action works using the colorWell as the sender. It doesn't work when I add it to a button initialized action:
NSColor *wellColor;
wellColor = [colorWell color];
float
red, green, blue;
red = [wellColor redComponent];
green = [wellColor greenComponent];
blue = [wellColor blueComponent];
[colorWell setColor:wellColor];
[colorWell setColor:[NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0]];
wellColor = [colorWell color];
NSRect r = NSMakeRect(190, 130, 100, 100);
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
NSColor *color = wellColor;
[color set];
[bp fill];
The simplest way is to use NSTextAttachment and attach it as an image (drawing a solid-color-filled image is a simple task you can learn about with the basic drawing documentation). Otherwise, you'll have to get into subclassing and customizing parts of the text system to make room for and draw the color there yourself.

Resources