forked from LiteCoder/DoubanFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorFunctions.cs
627 lines (602 loc) · 23.7 KB
/
ColorFunctions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Author : K.F.Storm
* Email : yk000123 at sina.com
* Website : http://www.kfstorm.com
* */
using System.Configuration;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
namespace DoubanFM
{
/// <summary>
/// 有关颜色的方法类
/// 主要用于计算窗口背景
/// </summary>
public class ColorFunctions
{
/// <summary>
/// 两种颜色不同的阀值
/// </summary>
public static readonly double CoverColorDiff = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.CoverColorDiff"]), 0.05);
/// <summary>
/// 亮度调整幅度
/// </summary>
public readonly static double ReviseParameter = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.ReviseParameter"]), 0.1);
/// <summary>
/// 用于调整封面右边取色区域的大小,具体表示右边区域宽度占图片宽度的百分比
/// </summary>
public readonly static double RightSideWidth = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.RightSideWidth"]), 0.1);
/// <summary>
/// 判断颜色是否太暗
/// </summary>
public readonly static double TooDark = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.TooDark"]), 0.15);
/// <summary>
/// 判断颜色是否太暗亮
/// </summary>
public readonly static double TooBright = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.TooBright"]), 0.4);
/// <summary>
/// 色相偏移
/// </summary>
public readonly static double HueOffset = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.HueOffset"]), 10);
/// <summary>
/// 进度条颜色调整参数
/// </summary>
public readonly static double ProgressBarReviseParameter = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.ProgressBarReviseParameter"]), 0.2);
/// <summary>
/// 判断颜色饱和度是否太低
/// </summary>
public readonly static double NotSaturateEnough = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.NotSaturateEnough"]), 0.4);
/// <summary>
/// 判断颜色饱和度是否接近0
/// </summary>
public readonly static double AlmostZeroSaturation = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.AlmostZeroSaturation"]), 0.001);
/// <summary>
/// 计算图片平均色时是否使用颜色的饱和度作权重
/// </summary>
public readonly static bool EnableColorWeight = GetValueOrDesignModeDefault(() => bool.Parse(ConfigurationManager.AppSettings["ColorFunctions.EnableColorWeight"]), true);
/// <summary>
/// 人脸的颜色
/// </summary>
public static readonly Color FaceColor = GetValueOrDesignModeDefault(() =>
(Color)ColorConverter.ConvertFromString(ConfigurationManager.AppSettings["ColorFunctions.FaceColor"]), Color.FromArgb(0xFF, 0xCD, 0xA3, 0x8F));
/// <summary>
/// 人脸色0权重的颜色差异阈值
/// </summary>
public static readonly double ZeroWeightFaceColorDifference = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.ZeroWeightFaceColorDifference"]), 0.03);
/// <summary>
/// 最低权重
/// </summary>
public static readonly double MinWeight = GetValueOrDesignModeDefault(() => double.Parse(ConfigurationManager.AppSettings["ColorFunctions.MinWeight"]), 0.2);
public delegate void ComputeCompleteCallback(Color color);
private static T GetValueOrDesignModeDefault<T>(Func<T> func, T designModeDefaultValue)
{
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
// In design mode
return designModeDefaultValue;
}
return func();
}
/// <summary>
/// 异步从图片中获取背景颜色
/// </summary>
/// <param name="image">图片</param>
/// <param name="callback">计算完成后调用的委托</param>
public static void GetImageColorForBackgroundAsync(BitmapSource image, ComputeCompleteCallback callback)
{
FormatConvertedBitmap bitmap = null;
bool isFrozen = image.IsFrozen;
if (!isFrozen)
{
//由于image没有冻结,所以image不能跨线程使用,这时要在当前线程中将image转换为另一个位图
bitmap = new FormatConvertedBitmap(image, PixelFormats.Rgb24, BitmapPalettes.WebPalette, 0);
}
ThreadPool.QueueUserWorkItem(state =>
{
if (isFrozen)
{
//由于image已经冻结,所以image可以跨线程使用,这时在新线程中将image转换为另一个位图
bitmap = new FormatConvertedBitmap(image, PixelFormats.Rgb24, BitmapPalettes.WebPalette, 0);
}
callback(GetImageColorForBackground(bitmap));
});
}
/// <summary>
/// 从图片中获取背景颜色
/// </summary>
/// <param name="bitmap">图片</param>
public static Color GetImageColorForBackground(FormatConvertedBitmap bitmap)
{
const int bytesPerPixel = 3;
if (bitmap.CanFreeze) bitmap.Freeze();
var pixels = new byte[bitmap.PixelHeight * bitmap.PixelWidth * bytesPerPixel];
bitmap.CopyPixels(pixels, bitmap.PixelWidth * bytesPerPixel, 0);
var width = bitmap.PixelWidth;
var height = bitmap.PixelHeight;
//计算颜色的均值
Color color = GetColorOfRegion(pixels, width, height, 0, width, 0, height);
var hsl = new HslColor(color);
if (IsNotSaturateEnough(hsl) && !IsAlmostZeroSaturation(hsl))
hsl.Saturation += 0.2;
return Revise(hsl).ToRgb();
}
/// <summary>
/// 从图片中获取指定区域的背景颜色
/// </summary>
/// <param name="pixels">The pixels.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="top">The top.</param>
/// <param name="bottom">The bottom.</param>
/// <param name="forceDisableColorWeight">if set to <c>true</c> [force disable color weight].</param>
/// <returns></returns>
public static Color GetColorOfRegion(byte[] pixels, int width, int height, int left, int right, int top,
int bottom, bool forceDisableColorWeight = false, bool removeFaceColor = true)
{
const int bytesPerPixel = 3;
double sr = 0, sg = 0, sb = 0;
double totalweight = 0;
for (int i = top; i < bottom; i++)
{
for (int j = left; j < right; j++)
{
byte r = pixels[(i * width + j) * bytesPerPixel + 0];
byte g = pixels[(i * width + j) * bytesPerPixel + 1];
byte b = pixels[(i * width + j) * bytesPerPixel + 2];
double weight;
if (!forceDisableColorWeight && EnableColorWeight)
{
var color = Color.FromRgb(r, g, b);
var hslColor = new HslColor(color);
weight = (1 - Math.Abs(1 - 2*hslColor.Lightness))*hslColor.Saturation;
if (weight < MinWeight)
{
weight = 0;
}
if (removeFaceColor)
{
var difference = Math.Abs(new HslColor(FaceColor).Hue - hslColor.Hue)/360;
if (difference <= ZeroWeightFaceColorDifference)
{
weight = 0;
}
else
{
weight = weight*difference;
}
}
}
else
{
weight = 1;
}
totalweight += weight;
sr += r * weight;
sg += g * weight;
sb += b * weight;
}
}
if (totalweight <= 0)
{
if (removeFaceColor)
{
//当去除人脸色彩后总权重为0时,禁用去除人脸色彩
return GetColorOfRegion(pixels, width, height, left, right, top, bottom, false, false);
}
else
{
//纯灰度图片不能使用权重
var newColor = GetColorOfRegion(pixels, width, height, left, right, top, bottom, true);
var newHslColor = new HslColor(newColor);
newHslColor.Saturation = 0;
return newHslColor.ToRgb();
}
}
else
{
sr = sr/totalweight;
sg = sg/totalweight;
sb = sb/totalweight;
return Color.FromRgb((byte) sr, (byte) sg, (byte) sb);
}
}
/// <summary>
/// 两种颜色差异是否足够大
/// </summary>
/// <param name="c1">颜色1</param>
/// <param name="c2">颜色2</param>
/// <returns>Boolean值</returns>
public static bool IsTooMuchDiff(HslColor c1, HslColor c2)
{
return Difference(c1, c2) > CoverColorDiff;
//return Math.Abs((c1.R - c2.R) * (c1.R - c2.R) + (c1.G - c2.G) * (c1.G - c2.G) + (c1.B - c2.B) * (c1.B - c2.B)) > CoverColorDiff;
}
/// <summary>
/// 计算两种颜色的差异。0为无差异,1为差异最大值
/// </summary>
/// <param name="c1">颜色1</param>
/// <param name="c2">颜色2</param>
/// <returns>差异值</returns>
public static double Difference(HslColor c1, HslColor c2)
{
return Difference(c1.ToRgb(), c2.ToRgb());
}
/// <summary>
/// 计算两种颜色的差异。0为无差异,1为差异最大值
/// </summary>
/// <param name="c1">颜色1</param>
/// <param name="c2">颜色2</param>
/// <param name="compareAlpha">是否比较Alpha通道</param>
/// <returns>
/// 差异值
/// </returns>
public static double Difference(Color c1, Color c2, bool compareAlpha = true)
{
if (compareAlpha)
{
return
Math.Sqrt((Math.Pow(c1.A - c2.A, 2) + Math.Pow(c1.R - c2.R, 2) + Math.Pow(c1.G - c2.G, 2) +
Math.Pow(c1.B - c2.B, 2))/4/255/255);
}
return Math.Sqrt((Math.Pow(c1.R - c2.R, 2) + Math.Pow(c1.G - c2.G, 2) + Math.Pow(c1.B - c2.B, 2))/3/255/255);
}
/// <summary>
/// 颜色饱和度是否太低
/// </summary>
/// <param name="color">颜色</param>
/// <returns>Boolean值</returns>
public static bool IsNotSaturateEnough(HslColor color)
{
return color.Saturation < NotSaturateEnough;
}
/// <summary>
/// 颜色饱和度是否接近0
/// </summary>
/// <param name="color">颜色</param>
/// <returns>Boolean值</returns>
public static bool IsAlmostZeroSaturation(HslColor color)
{
return color.Saturation < AlmostZeroSaturation;
}
/// <summary>
/// 颜色是否太暗
/// </summary>
/// <param name="color">颜色</param>
/// <returns>Boolean值</returns>
public static bool IsTooDark(HslColor color)
{
return color.Lightness < TooDark;
}
/// <summary>
/// 颜色是否太亮
/// </summary>
/// <param name="color">颜色</param>
/// <returns>Boolean值</returns>
public static bool IsTooBright(HslColor color)
{
return color.Lightness > TooBright;
}
/// <summary>
/// 反色
/// </summary>
/// <param name="color">原色</param>
/// <returns>反色</returns>
public static HslColor Reverse(HslColor color)
{
Color rgb = color.ToRgb();
return new HslColor(Color.FromArgb(rgb.A, (byte)(255 - rgb.R), (byte)(255 - rgb.G), (byte)(255 - rgb.B)));
//return new HSLColor(hsvColor.Alpha, hsvColor.Hue + 180, 1 - hsvColor.Saturation, 1 - hsvColor.Lightness);
}
/// <summary>
/// 颜色修正
/// </summary>
/// <param name="color1">待修正色</param>
/// <param name="color2">参照色</param>
/// <returns>修正色</returns>
public static HslColor Revise(HslColor color1, HslColor color2)
{
var newcolor = new HslColor(color1.ToRgb());
while (IsTooBright(newcolor) || !IsTooMuchDiff(newcolor, color2) && !IsTooDark(newcolor) && newcolor.Lightness > 0)
newcolor = ReviseDarker(newcolor);
if (!IsTooDark(newcolor)) return newcolor;
newcolor = ReviseBrighter(color1);
while (IsTooDark(newcolor) || !IsTooMuchDiff(newcolor, color2) && !IsTooBright(newcolor) && newcolor.Lightness < 1)
newcolor = ReviseBrighter(newcolor);
if (!IsTooBright(newcolor)) return newcolor;
if (IsTooBright(color1))
return ReviseVeryBright(color1);
if (IsTooDark(color1))
return ReviseVeryDark(color1);
return color1;
}
/// <summary>
/// 无参照色时的颜色修正
/// </summary>
/// <param name="color">待修正色</param>
/// <returns>修正色</returns>
public static HslColor Revise(HslColor color)
{
if (IsTooDark(color))
return ReviseBrighter(color);
if (IsTooBright(color))
return ReviseVeryBright(color);
//color = ReviseDarker(color);
//if (IsTooDark(color))
//return ReviseVeryDark(color);
return color;
}
/// <summary>
/// 将颜色调整到能够接受的最高亮度
/// </summary>
/// <param name="color">待修正色</param>
/// <returns>修正色</returns>
public static HslColor ReviseVeryBright(HslColor color)
{
return ReviseBrighter(color, TooBright - color.Lightness);
}
/// <summary>
/// 将颜色调整到能够接受的最低亮度
/// </summary>
/// <param name="color">待修正色</param>
/// <returns>修正色</returns>
public static HslColor ReviseVeryDark(HslColor color)
{
return ReviseDarker(color, color.Lightness - TooDark);
}
/// <summary>
/// 将颜色调亮特定亮度
/// </summary>
/// <param name="color">待修正色</param>
/// <param name="brigher">调整的亮度</param>
/// <returns>修正色</returns>
public static HslColor ReviseBrighter(HslColor color, double brigher)
{
return new HslColor(color.Alpha, color.Hue, color.Saturation, color.Lightness + brigher);
//return Color.FromRgb(ReviseByteBigger(hsvColor.R), ReviseByteBigger(hsvColor.G), ReviseByteBigger(hsvColor.B));
}
/// <summary>
/// 将颜色调亮一些
/// </summary>
/// <param name="color">待修正色</param>
/// <returns>修正色</returns>
public static HslColor ReviseBrighter(HslColor color)
{
return ReviseBrighter(color, ReviseParameter);
}
/// <summary>
/// 将颜色调暗特定亮度
/// </summary>
/// <param name="color">待修正色</param>
/// <param name="darker">调整的亮度</param>
/// <returns>修正色</returns>
public static HslColor ReviseDarker(HslColor color, double darker)
{
return new HslColor(color.Alpha, color.Hue, color.Saturation, color.Lightness - darker);
}
/// <summary>
/// 将颜色调暗一些
/// </summary>
/// <param name="color">待修正色</param>
/// <returns>修正色</returns>
public static HslColor ReviseDarker(HslColor color)
{
return ReviseDarker(color, ReviseParameter);
}
}
/// <summary>
/// HSL色彩空间的颜色类
/// </summary>
public class HslColor
{
public double a, h, s, l;
/// <summary>
/// 不透明度。范围:0~1,1为不透明,0为透明
/// </summary>
public double Alpha
{
get
{
return a;
}
set
{
if (value > 1)
a = 1;
else if (value < 0)
a = 0;
else a = value;
}
}
/// <summary>
/// 色相。范围:0~359.9999999。特殊颜色:红:0 黄:60 绿:120 青:180 蓝:240 洋红:300
/// </summary>
public double Hue
{
get
{
return h;
}
set
{
if (value >= 360)
h = value % 360;
else if (value < 0)
h = value - Math.Floor(value / 360) * 360;
else h = value;
}
}
/// <summary>
/// 饱和度。范围:0~1。亮度0.5时,0为灰色,1为彩色
/// </summary>
public double Saturation
{
get
{
return s;
}
set
{
if (value > 1)
s = 1;
else if (value < 0)
s = 0;
else s = value;
}
}
/// <summary>
/// 亮度。范围:0~1。0为黑色,0.5为彩色,1为白色
/// </summary>
public double Lightness
{
get
{
return l;
}
set
{
if (value > 1)
l = 1;
else if (value < 0)
l = 0;
else l = value;
}
}
/// <summary>
/// 无参构造函数
/// </summary>
public HslColor()
: this(1, 0, 0, 0)
{
}
/// <summary>
/// HSL构造函数
/// </summary>
/// <param name="hue">色相</param>
/// <param name="saturation">饱和度</param>
/// <param name="lightness">亮度</param>
public HslColor(double hue, double saturation, double lightness)
: this(1, hue, saturation, lightness)
{
}
/// <summary>
/// AHSL构造函数
/// </summary>
/// <param name="alpha">Alpha通道</param>
/// <param name="hue">色相</param>
/// <param name="saturation">饱和度</param>
/// <param name="lightness">亮度</param>
public HslColor(double alpha, double hue, double saturation, double lightness)
{
Alpha = alpha;
Hue = hue;
Saturation = saturation;
Lightness = lightness;
}
/// <summary>
/// 由RGB颜色类Color构造一个HSLColor的实例
/// </summary>
/// <param name="color">RGB颜色</param>
public HslColor(Color color)
{
FromRgb(color);
}
/// <summary>
/// RGB色彩空间转换
/// </summary>
/// <param name="color">RGB颜色</param>
public void FromRgb(Color color)
{
a = (double)color.A / 255;
double r = (double)color.R / 255;
double g = (double)color.G / 255;
double b = (double)color.B / 255;
double min = Math.Min(r, Math.Min(g, b));
double max = Math.Max(r, Math.Max(g, b));
double distance = max - min;
l = (max + min) / 2;
s = 0;
if (distance > 0)
{
s = l < 0.5 ? distance / (max + min) : distance / ((2 - max) - min);
double tempR = (((max - r) / 6) + (distance / 2)) / distance;
double tempG = (((max - g) / 6) + (distance / 2)) / distance;
double tempB = (((max - b) / 6) + (distance / 2)) / distance;
double hT;
if (r == max)
hT = tempB - tempG;
else if (g == max)
hT = (1.0 / 3 + tempR) - tempB;
else
hT = (2.0 / 3 + tempG) - tempR;
if (hT < 0)
hT += 1;
if (hT > 1)
hT -= 1;
h = hT * 360;
}
}
/// <summary>
/// 转换到RGB色彩空间
/// </summary>
/// <param name="hsl">HSL颜色</param>
/// <returns>转换后的RGB颜色</returns>
public static Color ToRgb(HslColor hsl)
{
byte a = (byte)Math.Round(hsl.Alpha * 255), r, g, b;
if (hsl.Saturation == 0)
{
r = (byte)Math.Round(hsl.Lightness * 255);
g = r;
b = r;
}
else
{
double vH = hsl.Hue / 360;
double v2 = hsl.Lightness < 0.5 ? hsl.Lightness * (1 + hsl.Saturation) : (hsl.Lightness + hsl.Saturation) - (hsl.Lightness * hsl.Saturation);
double v1 = 2 * hsl.Lightness - v2;
r = (byte)Math.Round(255 * HueToRgb(v1, v2, vH + 1.0 / 3));
g = (byte)Math.Round(255 * HueToRgb(v1, v2, vH));
b = (byte)Math.Round(255 * HueToRgb(v1, v2, vH - 1.0 / 3));
}
return Color.FromArgb(a, r, g, b);
}
/// <summary>
/// 这个……我也不懂
/// </summary>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <param name="vH"></param>
/// <returns></returns>
public static double HueToRgb(double v1, double v2, double vH)
{
if (vH < 0) vH += 1;
if (vH > 1) vH -= 1;
if (6 * vH < 1) return v1 + ((v2 - v1) * 6 * vH);
if (2 * vH < 1) return v2;
if (3 * vH < 2) return v1 + (v2 - v1) * (2.0 / 3 - vH) * 6;
return v1;
}
/// <summary>
/// 转换到RGB色彩空间
/// </summary>
/// <returns>RGB颜色</returns>
public Color ToRgb()
{
return ToRgb(this);
}
/// <summary>
/// ToString()方法,已重写
/// </summary>
/// <returns>ARGB颜色值</returns>
public override string ToString()
{
return ToRgb().ToString();
}
}
}