I'm having problems constructing a CGColor within an index based CGColorSpace using this constructor. components parameter takes 2-element array - an index of a color in the color space and alpha. The API seems to round the CGFloat index value to the nearest integer as well as caps it at the last value of the color space.
I am able to create only 2 colors from the color space table - either the first, when index is 0 or the last, when index is >= 1. How to I create any other color from the color space?
Here is sample code for reference:
let baseColorSpace = CGColorSpaceCreateDeviceRGB()
let colorSpace = CGColorSpace(
indexedBaseSpace: baseColorSpace,
last: 4,
colorTable: [
0x73, 0x1d, 0xd8,
0x48, 0xa9, 0xa6,
0xe4, 0xdf, 0xda,
0xd4, 0xb4, 0x83,
0xc1, 0x66, 0x6b
])!
guard let color1 = CGColor(colorSpace: colorSpace, components: [2.0, 1.0]),
let color2 = color1.converted(to: baseColorSpace, intent: .defaultIntent, options: nil) else {
fatalError("Failed to convert color to RGB color space.")
}
print(color1.components)
print(color2.components)
I've tried setting index component to random values in different ranges.