我有2种颜色:1种动态设置,另一种颜色始终为0.5 alpha。我想计算得到的白色,就像使用叠加混合模式在动态颜色上绘制一样。
我知道叠加结合了 Multiply 和屏幕混合模式。
乘法混合模式的公式为:
Result Color = (Top Color) * (Bottom Color) /255
屏幕混合模式为:
Result Color = 255 - [((255 - Top Color)*(255 - Bottom Color))/255]
如何计算叠加混合模式的最终颜色?
是否有UIColor
扩展类可以开箱即用?
答案 0 :(得分:7)
公式有两部分:
第一部分:如果较低层值> 127.5,然后执行以下操作 -
值单位=(255-较低层值)/127.5
最小值=较低层值 - (255-较低层值)
覆盖=(上层值*值单位)+最小值
第二部分:如果较低层值< 127.5,然后执行以下操作 -
价值单位=较低层值/ 127.5
叠加=上层值*值单位
从形式上我们可以看出最终结果很大程度上取决于上层值。如果上层值更高(更轻),则最终结果更可能更轻。
来自here。
答案 1 :(得分:7)
跟进willi's answer,这是移植到代码的公式:
CGFloat newComponents[4];
const CGFloat *topComponents = CGColorGetComponents([[UIColor colorWithRed:1 green:1 blue:1 alpha:1] CGColor]);
const CGFloat *components = CGColorGetComponents(self.color.CGColor);
const int n = CGColorGetNumberOfComponents(self.color.CGColor);
for(int i=0; i < n; i++) {
if(components[i] > 0.5) {
CGFloat value = (topComponents[i]-components[i])/0.5;
CGFloat min = components[i]-(topComponents[i]-components[i]);
newComponents[i] = topComponents[i]*value+min;
} else {
CGFloat value = components[i]/0.5;
newComponents[i] = topComponents[i]*value;
}
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *resultColor = [UIColor colorWithCGColor:CGColorCreate(colorSpace, newComponents)];
CGColorSpaceRelease(colorSpace);
答案 2 :(得分:2)
我不知道你的目标,可能完全偏离主题,但为什么不只是使用Quartz 2D?
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
... draw with overlay blending
CGBlendMode
通过CGContextSetBlendMode
覆盖,乘法,屏幕等提供......:
enum CGBlendMode {
/* Available in Mac OS X 10.4 & later. */
kCGBlendModeNormal,
kCGBlendModeMultiply,
kCGBlendModeScreen,
kCGBlendModeOverlay,
kCGBlendModeDarken,
kCGBlendModeLighten,
kCGBlendModeColorDodge,
kCGBlendModeColorBurn,
kCGBlendModeSoftLight,
kCGBlendModeHardLight,
kCGBlendModeDifference,
kCGBlendModeExclusion,
kCGBlendModeHue,
kCGBlendModeSaturation,
kCGBlendModeColor,
kCGBlendModeLuminosity,
/* Available in Mac OS X 10.5 & later. R, S, and D are, respectively,
premultiplied result, source, and destination colors with alpha; Ra,
Sa, and Da are the alpha components of these colors.
The Porter-Duff "source over" mode is called `kCGBlendModeNormal':
R = S + D*(1 - Sa)
Note that the Porter-Duff "XOR" mode is only titularly related to the
classical bitmap XOR operation (which is unsupported by
CoreGraphics). */
kCGBlendModeClear, /* R = 0 */
kCGBlendModeCopy, /* R = S */
kCGBlendModeSourceIn, /* R = S*Da */
kCGBlendModeSourceOut, /* R = S*(1 - Da) */
kCGBlendModeSourceAtop, /* R = S*Da + D*(1 - Sa) */
kCGBlendModeDestinationOver, /* R = S*(1 - Da) + D */
kCGBlendModeDestinationIn, /* R = D*Sa */
kCGBlendModeDestinationOut, /* R = D*(1 - Sa) */
kCGBlendModeDestinationAtop, /* R = S*(1 - Da) + D*Sa */
kCGBlendModeXOR, /* R = S*(1 - Da) + D*(1 - Sa) */
kCGBlendModePlusDarker, /* R = MAX(0, (1 - D) + (1 - S)) */
kCGBlendModePlusLighter /* R = MIN(1, S + D) */
};
typedef enum CGBlendMode CGBlendMode; /* Available in Mac OS X 10.4 & later. */