I have colorA which is [UIColor blueColor], and colorB, which is [UIColor redColor]. Is this possible for me to render a [UIColor purple]? How can it be implemented? Thanks.
Inelegant, but it works:
UIColor* blend( UIColor* c1, UIColor* c2, float alpha )
{
alpha = MIN( 1.f, MAX( 0.f, alpha ) );
float beta = 1.f - alpha;
CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
[c1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
[c2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
CGFloat r = r1 * beta + r2 * alpha;
CGFloat g = g1 * beta + g2 * alpha;
CGFloat b = b1 * beta + b2 * alpha;
return [UIColor colorWithRed:r green:g blue:b alpha:1.f];
}
More elegant:
UIColor+Extensions.h:
#interface UIColor (Extensions)
- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2;
#end
UIColor+Extensions.m:
#implementation UIColor (Extensions)
- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2
{
alpha2 = MIN( 1.0, MAX( 0.0, alpha2 ) );
CGFloat beta = 1.0 - alpha2;
CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
[self getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
[color2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
CGFloat red = r1 * beta + r2 * alpha2;
CGFloat green = g1 * beta + g2 * alpha2;
CGFloat blue = b1 * beta + b2 * alpha2;
CGFloat alpha = a1 * beta + a2 * alpha2;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
#end
Swift 3/4:
extension UIColor
{
func mixin(infusion:UIColor, alpha:CGFloat) -> UIColor {
let alpha2 = min(1.0, max(0, alpha))
let beta = 1.0 - alpha2
var r1:CGFloat = 0, r2:CGFloat = 0
var g1:CGFloat = 0, g2:CGFloat = 0
var b1:CGFloat = 0, b2:CGFloat = 0
var a1:CGFloat = 0, a2:CGFloat = 0
if getRed(&r1, green: &g1, blue: &b1, alpha: &a1) &&
infusion.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
{
let red = r1 * beta + r2 * alpha2;
let green = g1 * beta + g2 * alpha2;
let blue = b1 * beta + b2 * alpha2;
let alpha = a1 * beta + a2 * alpha2;
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
// epique de las failuree
return self
}
}
Ask each input color for its RGBA components using -[UIColor getRed:green:blue:alpha:]. Average the components from each, and create a new color using +[UIColor colorWithRed:green:blue:alpha:].
Swift5 UIColor Extension
extension UIColor {
func add(_ overlay: UIColor) -> UIColor {
var bgR: CGFloat = 0
var bgG: CGFloat = 0
var bgB: CGFloat = 0
var bgA: CGFloat = 0
var fgR: CGFloat = 0
var fgG: CGFloat = 0
var fgB: CGFloat = 0
var fgA: CGFloat = 0
self.getRed(&bgR, green: &bgG, blue: &bgB, alpha: &bgA)
overlay.getRed(&fgR, green: &fgG, blue: &fgB, alpha: &fgA)
let r = fgA * fgR + (1 - fgA) * bgR
let g = fgA * fgG + (1 - fgA) * bgG
let b = fgA * fgB + (1 - fgA) * bgB
return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}
static func +(lhs: UIColor, rhs: UIColor) -> UIColor {
return lhs.add(rhs)
}
}
Usage
let opacity: CGFloat = 0.6
let start = UIColor.red
let end = UIColor.blue
let combined = start.withAlphaComponent(opacity) + end.withAlphaComponent(1-opacity)
Since it is 2020 now, and we have "Dark Mode" available, many "colours" are not just colours anymore, but automatically adapt to color changes. They are created by calling UIColor(dynamicProvider: ...).
You should check if both colors are dynamic colors, and if they are, return a dynamic color. Mixing one dynamic color and a non-dynamic color doesn't really make sense, so in that case return a non-dynamic color.
You can't mix two UIColors directly as you mention in your question. Anyways there is an example to mix two colors using other schemes. The link for same is as follows:
Link Here
If you want to use purple color then its not a big deal.All the UIColor are rgb color.You can simply pass the RGB value for each color and can get the desired color.Use this link to convert hex value of any color to convert in UIColor.
http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/
like #color code for green is #008400:
[UIColor colorWithRed:0 green:0.518 blue:0 alpha:1] /#008400/
I am trying to get a random color. I have done it using brute force but this method seems overly laborious (though the distribution is pretty even):
- (UIColor *) getRandomColor {
// GOAL: reject colors that are too dark
float total = 3;
float one = arc4random() % 256 / 256.0;
total -= one;
float two = arc4random() % 256 / 256.0;
total -= two;
float three = total; // UIColor will chop out-of-range nums
NSMutableArray *threeFloats = [[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:one], [NSNumber numberWithFloat:two], [NSNumber numberWithFloat:three], nil] autorelease];
NSNumber *red, *green, *blue;
red = [threeFloats objectAtIndex:arc4random() % [threeFloats count]];
[threeFloats removeObject:red];
green = [threeFloats objectAtIndex:arc4random() % [threeFloats count]];
[threeFloats removeObject:green];
blue = [threeFloats lastObject];
return [UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:1];
}
How can it be bettered? I want an even distribution of red, green and blue and nothing too dark (otherwise I'd grab three random numbers and be done with it).
+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
If white is ok then do saturation like hue instead of brightness.
I'd probably generate (pseudo-)random numbers for hue, saturation and lightness (or HSB), with the lightness (or brightness) limited to whatever range you find reasonable, then convert that to RGB.
This method might help you quickly generate bright colors randomly:
1 - Pick a random integer between 0-2
2 - If it is 0, set Red to Random(0.75-1.0), Green and Blue to Random(0.0-1.0)
2 - If it is 1, set Green to Random(0.75-1.0), Red and Blue to Random(0.0-1.0)
2 - If it is 2, set Blue to Random(0.75-1.0), Green and Red to Random(0.0-1.0)
How do I generate a random color hexadecimal in Objective-C ?
I need a color hexdecimal , I don't need a random color. It is complicated ...
I think should work for you. Arc4random() is far better in the way of performance & ... accuracy than rand(). Rand() also needs to be seeded before use.
// 16777215 is FFFFFF
NSInteger *baseInt = arc4random() % 16777216;
NSString *hex = [NSString stringWithFormat:#"%06X", baseInt];
Edit: Edited based on comment regarding formatting.
You can use the standard C library routine rand() in your Objective-C application. From there, then, you'll want to call it three times to get random values for each of the red, green, and blue channels of your random color. You'll want to mod (%) the value by the maximum value the channel can have- typically 256. From there you can construct your NSColor appropriately. So your code might look something like:
int red = rand() % 255;
int green = rand() % 255;
int blue = rand() % 255;
NSColor* myColor = [NSColor colorWithCalibratedRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
Because NSColor takes floats instead of integers a better approach would be to divide the random values by RAND_MAX right from the start:
float rand_max = RAND_MAX;
float red = rand() / rand_max;
float green = rand() / rand_max;
float blue = rand() / rand_max;
NSColor* myColor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0];
This latter code will not limit the number of colors to a 24-bit spectrum.
There is an article on cocoadev (including code examples) on writing a screensaver - with random colors:
http://cocoadevcentral.com/articles/000088.php
Just for a short note:
Depending on your needs, it's possible that you should consider to write a not-so-random color generator. (For example when want to set up "rules" for the visibility of the generated colors.) So it's possible that you need to get the color code by limiting the random-generated values in HSB.
I am getting a UIColor returned from this method:
- (UIColor *)getUserSelectedColor {
return [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1.0];
}
and getting color like this:
UIColor *selectedColor = [(ColorPickerView *)alertView getUserSelectedColor];
Now I want to get red, green, blue from selectedColor, in order to use those values. I want values between 0 and 1.
The reason for the crash when accessing SelectedColor.CGColor could be that you do not retain the result from getColor, perhaps what you need is:
SelectedColor = [[(ColorPickerView *)alertView getColor] retain];
You can only get the RGB color component from a UIColor that is using the RGB color space, since you are using colorWithRed:green:blue:alpha: that is not a problem, but be vary of this if your code changes.
With this is mind getting the color components is really easy:
const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor);
NSLog(#"Red: %f", components[0]);
NSLog(#"Green: %f", components[1]);
NSLog(#"Blue: %f", components[2]);
NSLog(#"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor));
This solution works for non-RGB colours as well e.g. black or white color.
UIColor *color = [UIColor blackColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
// iOS 5
if ([color respondsToSelector:#selector(getRed:green:blue:alpha:)]) {
[color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
// < iOS 5
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
// This is a non-RGB color
if(CGColorGetNumberOfComponents(color.CGColor) == 2) {
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
}
There is a Swift extension for that :)
extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 0.0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red: red, green: green, blue: blue, alpha: alpha)
}
var redComponent: CGFloat {
var red: CGFloat = 0.0
getRed(&red, green: nil, blue: nil, alpha: nil)
return red
}
var greenComponent: CGFloat {
var green: CGFloat = 0.0
getRed(nil, green: &green, blue: nil, alpha: nil)
return green
}
var blueComponent: CGFloat {
var blue: CGFloat = 0.0
getRed(nil, green: nil, blue: &blue, alpha: nil)
return blue
}
var alphaComponent: CGFloat {
var alpha: CGFloat = 0.0
getRed(nil, green: nil, blue: nil, alpha: &alpha)
return alpha
}
}
It is compatible with Swift 4.2 and also works with 2 components colors like black, gray, etc.
You can access a specific canal like so :
myColor.rgba.blue
Or, its equivalent :
myColor.blueComponent
In most cases this will work, unless the conversion to RGB doesn't work.
float red, green, blue, alpha;
BOOL conversionToRGBWentOk = [color getRed:&red green:&green blue:&blue alpha:&alpha];
That's what these methods are for, in fact. If the conversionToRGBWentOk is NO you'll have a problem, though.
I think you should have a a look here, where Ars' guide shows how to extend the UIColor class with support for accessing the color components.
you just simple doing this
CGFloat red,green,blue,alpha;
[UIColorobject getRed:&red green:&green blue:&blue alpha:&alpha];
in red,green,blue and alpha you get rgb value
if you have any question please ask...
Thanks
This snippet of code should work with both RGB and grayscale:
CGFloat *components = (CGFloat *) CGColorGetComponents(<UIColor instance>.CGColor);
if(CGColorGetNumberOfComponents(<UIColor instance>.CGColor) == 2)
{
//assuming it is grayscale - copy the first value
components[2] = components[1] = components[0];
}
I think this would be a way to go. If you need to use alpha parameter as well, you can interpolate alpha from input like you do for R G and B.
- (UIColor *)getColorBetweenColor:(UIColor *)color1 andColor:(UIColor *)color2 percentage:(CGFloat)percent {
CGFloat red1, green1, blue1, alpha1;
CGFloat red2, green2, blue2, alpha2;
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
[color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
double resultRed = red1 + percent * (red2 - red1);
double resultGreen = green1 + percent * (green2 - green1);
double resultBlue = blue1 + percent * (blue2 - blue1);
return [UIColor colorWithRed:resultRed green:resultGreen blue:resultBlue alpha:1];
}
Just add the property ColorLiteral as shown in the example. Xcode will prompt you with a whole list of colors which you can choose.
self.view.backgroundColor = ColorLiteral
Here are some useful macros I've made for this and other color controls:
In your case you would just use
getRGBA(myColor, red, green, blue, alpha);
NSLog(#"Red Value: %f", red);
NSLog(#"Blue Value: %f", green);
NSLog(#"Green Value: %f", blue);
Macros:
#define rgba(r,g,b,a) [UIColor colorWithRed:((float)(r))/255.0f green:((float)(g))/255.0f blue:((float)(b))/255.0f alpha:a]
#define rgb(r,g,b) rgba(r, g, b, 1.0f)
#define rgbaf(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:a]
#define rgbf(r,g,b) rgbaf(r, g, b, 1.0f)
#define rgba_fromColor(__color, __r, __g, __b, __a) \
CGFloat __r, __g, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getRed:&__r green:&__g blue:&__b alpha:&__a];
#define getRGBA(__color, __r, __g, __b, __a) rgba_fromColor(__color, __r, __g, __b, __a)
#define getRed(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return r;\
})()\
)
#define getGreen(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return g;\
})()\
)
#define getBlue(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return b;\
})()\
)
#define getAlpha(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return a;\
})()\
)
#define hsba(h,s,b,a) [UIColor colorWithHue:((float)(h))/360.0f saturation:((float)(s))/100.0f brightness:((float)(b))/100.0f alpha:a]
#define hsb(h,s,b) hsba(h, s, b, 1.0f)
#define hsbaf(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:a]
#define hsbf(h,s,b) rgbaf(h, s, b, 1.0f)
#define hsba_fromColor(__color, __h, __s, __b, __a) \
CGFloat __h, __s, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getHue:&__h saturation:&__s brightness:&__b alpha:&__a];
#define getHSBA(__color, __h, __s, __b, __a) hsba_fromColor(__color, __h, __s, __b, __a)
#define getHue(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return h;\
})()\
)
#define getSaturation(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return s;\
})()\
)
#define getBrightness(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return b;\
})()\
)
/*
///already defined in RGBA macros
#define getAlpha(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return a;\
})()\
)
*/