排序颜色(Objective-C)

前端之家收集整理的这篇文章主要介绍了排序颜色(Objective-C)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在做这样的事情:
- (NSArray*)colors {
    float divisor = .3333;
    NSMutableArray *retVal = [NSMutableArray array];
    for (float one=0; one <= 1.0f; one += divisor) {
        for (float two = 0; two <= 1.0f; two += divisor) {
            for (float three = 0; three <= 1.0f; three += divisor) {
                UIColor *color = [UIColor colorWithRed:one green:two blue:three alpha:.5];
                // also bad
                // UIColor *color = [UIColor colorWithHue:one saturation:two brightness:three alpha:.5];
                [retVal addObject:color];
            }
        }
    }
    return retVal;
}

正如我所怀疑的那样,这些颜色出乎意料地出现了(眼中).红色不带红色,紫色不用紫色等.

有没有简单的方法来创建不同颜色的列表,根据人类标准,“看起来很蓝色”很好地分组.

解决方法

这工作得很好.它不会帮助你有很多重复的颜色的事实.见下文:
NSArray *sorted = [[dict allValues] sortedArrayUsingComparator:^NSComparisonResult(UIColor* obj1,UIColor* obj2) {
    float hue,saturation,brightness,alpha;
    [obj1 getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    float hue2,saturation2,brightness2,alpha2;
    [obj2 getHue:&hue2 saturation:&saturation2 brightness:&brightness2 alpha:&alpha2];
    if (hue < hue2)
        return NSOrderedAscending;
    else if (hue > hue2)
        return NSOrderedDescending;

    if (saturation < saturation2)
        return NSOrderedAscending;
    else if (saturation > saturation2)
        return NSOrderedDescending;

    if (brightness < brightness2)
        return NSOrderedAscending;
    else if (brightness > brightness2)
        return NSOrderedDescending;

    return NSOrderedSame;
}];

您可以在iOS 4.x中访问这些组件(HSBA):

CGFloat *components = (CGFloat *)CGColorGetComponents([color CGColor]);
    float hue = components[0];
    float saturation = components[1]; // etc. etc.

为了避免重复颜色:您可以将元素放在NSMutableDictionary中,键入类似于它们的色相饱和度亮度(每个四舍五入到最接近的.10)…然后从THAT获得数组,然后进行排序.

猜你在找的C&C++相关文章