Calculate the color at a given point on a gradient between two colors?
So this is essentially the method I would like to write (in Objective-C/Cocoa, using
UIColors, but I’m really just interested in the underlying math):
+ (UIColor *)colorBetweenColor:(UIColor *)startColor andColor:(UIColor *)endColor atLocation:(CGFloat)location;
So as an example, say I have two colors, pure red and pure blue. Given a linear gradient between the two, I want to calculate the color that’s at, say, the 33% mark on that gradient:
So if I were to call my method like so:
UIColor *resultingColor = [UIColor colorBetweenColor:[UIColor redColor] andColor:[UIColor blueColor] atLocation:0.33f];
I would get the resulting color at ‘B’, and similarly, passing
0.0f as the location would return color ‘A’, and
1.0f would return color ‘C’.
So basically my question is, how would I go about mixing the RGB values of two colors and determining the color at a certain ‘location’ between them?
- Is it possible to get a colour name in iOS based on RGB values?
- How to create Named colors in Interface Builder?
- How to define colors in XCodes' Interface Builder?
- How to calculate the average color of a UIImage?
- Is there a way color-code the iPhone Debugger Console display in Xcode?
- Color detection at a distance (Swift)
2 Solutions Collect From Internet About “Calculate the color at a given point on a gradient between two colors?”
You simply linearly interpolate the red, the green, and the blue channels like this:
double resultRed = color1.red + percent * (color2.red - color1.red); double resultGreen = color1.green + percent * (color2.green - color1.green); double resultBlue = color1.blue + percent * (color2.blue - color1.blue);
percent is a value between 0 and 1 (
location in your first method prototype).
RGB color space is like a circle. With highest saturation along the outer border, and grey in the middle. Traveling from one color to another, you’d preferably want to be doing that, along the same radius (circle) from the middle; so as to where saturation and value stay the same. In that case, the hue is changing in a linear fashion. You will not cross into more grey area than your left and right colors initially are.
You can travel from an inner ring to an outer ring, simple go up (or down) the saturation; again linearly.
See here for color circle (try it in e.g. paint.net)
Apple’s (iOS) objective classes allow you to work with other spectra than RGB.
- How to display superscript for third power/cube characters as a string in UILabel?
- when using loadNibNamed why do we have to use 'first'? Don't we want to decouple different entities of our source code?
- How to compare two UIImage objects
- Views Navigation Using Swipe Gesture
- NSOperationQueue vs GCD
- iPhone App : recording audio in mp3
- IOS: How to print using an IPad
- iPhone — is it possible to inspect the frame of a UIView in the Xcode debugger?
- Get Path to Subdirectory in Resources Folder
- Cannot open/create SQLite database in iOS
- Avoid consecutive “if let” declarations in Swift
- IBOutlet is nil, but it is connected in storyboard, Swift
- Why can we not cast to protocol types with associated types but achieve the same effect using generics?
- Swift: Array find by identity
- Which CGImageAlphaInfo should we use?