我有一大堆颜色要在我的UI中显示在一个数组中,我认为如果它们按颜色排序会看起来最好。我该怎么做呢?
答案 0 :(得分:9)
如何根据色调对颜色进行排序:
private void DrawPictureBox()
{
pbScreen.Image = Update();
}
private Bitmap CreateBackgroundBitmap()
{
Bitmap bitmap = (Bitmap)Properties.Resources.ResourceManager.GetObject("empty");
bitmap.MakeTransparent(Color.White);
return bitmap;
}
private ImageAttributes GetImageAttributes()
{
float[][] matrixItems = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, Contrast, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
return imageAtt;
}
private void DrawSegment(ref Graphics g, Digit digit, int Position = 0)
{
if (digit == null)
return;
Bitmap buffer = digit.CreateBitmapFromFile(digit.CreateFileNameFromContent());
buffer.MakeTransparent(Color.White);
g.DrawImage(buffer, new Rectangle(0 + Position * 43, 0, 43, 67), 0.0f, 0.0f, 43, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
}
public Bitmap Update()
{
Bitmap buffer = CreateBackgroundBitmap();
Graphics g = Graphics.FromImage(buffer);
g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
if (State == Powerstate.on)
{
// Draw segments
for (int i = 0; i < digits.Count(); i++)
{
DrawSegment(ref g, digits[i], i);
if (digits[i]?.Dot == Dot.dot_on)
{
DrawPoint(ref g, digits[i], i);
}
}
}
return buffer;
}
西蒙
答案 1 :(得分:3)
除非您定义哪种颜色比另一种颜色更大或更小,否则无法以您想要的方式对颜色数组进行排序。对于例如你不能说红色比蓝色大。
您将需要定义可用作排序描述符的颜色组件。例如,如果您希望根据红色组件或绿色或蓝色组件对颜色数组进行排序。
因此,您唯一的选择是使用颜色对象的一些随机特性作为排序描述符。一种方法是使用颜色对象的hashValue
并使用它进行排序。我不确定系统如何计算UIColor对象的hashValue。所以你可以像这样对颜色数组进行排序。
var colors = [UIColor.redColor(), UIColor.orangeColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.cyanColor(), UIColor.magentaColor()]
let sortedColors = colors.sort { (color1, color2) -> Bool in
color1.hashValue <= color2.hashValue
}
如果你想用红色排序,然后是绿色,然后是蓝色然后是alpha,那么你可以使用这个排序功能。
let sortedColors = colors.sort { (color1, color2) -> Bool in
var red1:CGFloat=0, red2:CGFloat=0
var green1:CGFloat=0, green2:CGFloat=0
var blue1:CGFloat=0, blue2:CGFloat=0
var alpha1:CGFloat=0, alpha2:CGFloat=0
color1.getRed(&red1, green: &green1, blue: &blue1, alpha: &alpha1)
color2.getRed(&red2, green: &green2, blue: &blue2, alpha: &alpha2)
if red1 == red2 {
if green1 == green2 {
if blue1 == blue2 {
return alpha1 < alpha2
} else {
return blue1 < blue2
}
} else {
return green1 < green2
}
} else {
return red1 < red2
}
}
答案 2 :(得分:0)
试试这个。
//
// 1. make category
//
@interface UIColor(myColor)
- (CGFloat) red;
- (CGFloat) green;
- (CGFloat) blue;
- (CGFloat) alpha;
@end
@implementation UIColor (myColor)
- (CGFloat) red {
CGFloat red;
CGFloat blue;
CGFloat green;
CGFloat alpha;
[self getRed: &red green: &green blue: &blue alpha: &alpha];
return red;
}
- (CGFloat) green {
CGFloat red;
CGFloat blue;
CGFloat green;
CGFloat alpha;
[self getRed: &red green: &green blue: &blue alpha: &alpha];
return green;
}
- (CGFloat) blue {
CGFloat red;
CGFloat blue;
CGFloat green;
CGFloat alpha;
[self getRed: &red green: &green blue: &blue alpha: &alpha];
return blue;
}
- (CGFloat) alpha {
CGFloat red;
CGFloat blue;
CGFloat green;
CGFloat alpha;
[self getRed: &red green: &green blue: &blue alpha: &alpha];
return alpha;
}
@end
//
// 2. sort
//
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray: @[[UIColor blackColor],
[UIColor darkGrayColor],
[UIColor lightGrayColor],
[UIColor whiteColor],
[UIColor grayColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor cyanColor],
[UIColor yellowColor],
[UIColor magentaColor],
[UIColor orangeColor],
[UIColor purpleColor],
[UIColor brownColor],
[UIColor clearColor]]];
NSLog(@"before sorted" );
for(UIColor* color in array) {
NSLog(@"r:%.05f, g:%.05f, b:%.05f, a:%.05f", color.red, color.green, color.blue, color.alpha);
}
NSSortDescriptor *sortDescriptor00 = [[NSSortDescriptor alloc] initWithKey: @"red" ascending: YES];
NSSortDescriptor *sortDescriptor01 = [[NSSortDescriptor alloc] initWithKey: @"green" ascending: YES];
NSSortDescriptor *sortDescriptor02 = [[NSSortDescriptor alloc] initWithKey: @"blue" ascending: YES];
NSSortDescriptor *sortDescriptor03 = [[NSSortDescriptor alloc] initWithKey: @"alpha" ascending: YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor00, sortDescriptor01, sortDescriptor02, sortDescriptor03, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors: sortDescriptors];
NSLog(@"\nafter sorted" );
for(UIColor* color in sortedArray) {
NSLog(@"r:%.05f, g:%.05f, b:%.05f, a:%.05f", color.red, color.green, color.blue, color.alpha);
}
before sorted
r:0.00000, g:0.00000, b:0.00000, a:1.00000
r:0.33333, g:0.33333, b:0.33333, a:1.00000
r:0.66667, g:0.66667, b:0.66667, a:1.00000
r:1.00000, g:1.00000, b:1.00000, a:1.00000
r:0.50000, g:0.50000, b:0.50000, a:1.00000
r:1.00000, g:0.00000, b:0.00000, a:1.00000
r:0.00000, g:1.00000, b:0.00000, a:1.00000
r:0.00000, g:0.00000, b:1.00000, a:1.00000
r:0.00000, g:1.00000, b:1.00000, a:1.00000
r:1.00000, g:1.00000, b:0.00000, a:1.00000
r:1.00000, g:0.00000, b:1.00000, a:1.00000
r:1.00000, g:0.50000, b:0.00000, a:1.00000
r:0.50000, g:0.00000, b:0.50000, a:1.00000
r:0.60000, g:0.40000, b:0.20000, a:1.00000
r:0.00000, g:0.00000, b:0.00000, a:0.00000
after sorted
r:0.00000, g:0.00000, b:0.00000, a:0.00000
r:0.00000, g:0.00000, b:0.00000, a:1.00000
r:0.00000, g:0.00000, b:1.00000, a:1.00000
r:0.00000, g:1.00000, b:0.00000, a:1.00000
r:0.00000, g:1.00000, b:1.00000, a:1.00000
r:0.33333, g:0.33333, b:0.33333, a:1.00000
r:0.50000, g:0.00000, b:0.50000, a:1.00000
r:0.50000, g:0.50000, b:0.50000, a:1.00000
r:0.60000, g:0.40000, b:0.20000, a:1.00000
r:0.66667, g:0.66667, b:0.66667, a:1.00000
r:1.00000, g:0.00000, b:0.00000, a:1.00000
r:1.00000, g:0.00000, b:1.00000, a:1.00000
r:1.00000, g:0.50000, b:0.00000, a:1.00000
r:1.00000, g:1.00000, b:0.00000, a:1.00000
r:1.00000, g:1.00000, b:1.00000, a:1.00000