从 UIColor 获取 MidColor [英] Get MidColor from UIColor

查看:20
本文介绍了从 UIColor 获取 MidColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以从传递的 UIColor 中给我中间颜色的示例.在下面的示例,从 UIColor 获取更浅和更深的颜色
可以变得更轻&UIColor 颜色较深.但我需要从 UIColor 获得平均/中间颜色.

I was looking for sample which gives me mid colour from passed UIColor. In the following Example, Get Slightly Lighter and Darker Color from UIColor
can get lighter & darker colour from UIColor. But i need to get average/ mid colour from UIColor.

有人帮我解决这个问题吗?提前致谢.

Anyone help me to solve this? Thanks in Advance.

推荐答案

您指向的代码在第一个代码示例中将颜色向上或向下步进 0.2,并在第二个代码示例中调整亮度.

The code to which you point steps the colour up or down by 0.2 in the first code example and adjusts brightness in the second.

我认为中间色是指以下两种情况之一:(1) 颜色本身,或 (2) 不像示例代码那样浅或深的颜色.要实现后者,您需要做的就是更改亮度乘数,例如

By mid-colour I assume you mean one of two things: (1) the colour itself, or (2) something that isn't quite as light or quite as dark as the example code. To achieve the latter, all you need to do is change the brightness multiplier, e.g.

@implementation UIColor (LightAndDark)

- (UIColor *)lighterColor
{
    CGFloat h, s, b, a;
    if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
        return [UIColor colorWithHue:h
                          saturation:s
                          brightness:MIN(b * 1.15, 1.0)
                               alpha:a];
    return nil;
}

- (UIColor *)darkerColor
{
    CGFloat h, s, b, a;
    if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
        return [UIColor colorWithHue:h
                          saturation:s
                          brightness:b * 0.85
                               alpha:a];
    return nil;
}
@end

还是您正在寻找介于两种颜色之间的颜色?现在处理此问题将很快再次编辑.

or is it that you are looking for a colour between two colours? Working on this now will edit again shortly.

更新:使用中间色:

- (UIColor *)betweenColor:(UIColor *)c andColor:(UIColor *)d
{
    CGFloat r, g, b, a;
    CGFloat r2, g2, b2, a2;
    [c getRed:&r green:&g blue:&b alpha:&a];
    [d getRed:&r2 green:&g2 blue:&b2 alpha:&a2];

    return [UIColor colorWithRed:(r + r2)/2
                           green:(g + g2)/2
                            blue:(b + b2)/2
                           alpha:a];
    return nil;
}

并这样称呼它:

UIColor *midColor = [self betweenColor:[UIColor whiteColor] andColor:[UIColor blackColor]];
self.view.backgroundColor = midColor;

这里是 HSB 的中点:

and here's the HSB midpoint:

- (UIColor *)betweenColor:(UIColor *)c andColor:(UIColor *)d
{
    CGFloat h, s, b, a;
    CGFloat h2, s2, b2, a2;
    [c getHue:&h saturation:&s brightness:&b alpha:&a];
    [d getHue:&h2 saturation:&s2 brightness:&b2 alpha:&a2];
    return [UIColor colorWithHue:(h+h2)/2
                      saturation:(s+s2)/2
                      brightness:(b+b2)/2
                           alpha:a];    return nil;
}

这篇关于从 UIColor 获取 MidColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆