PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/BaconographyWP8Core/Converters/DepthColorConverter.cs

https://github.com/hippiehunter/Baconography
C# | 153 lines | 142 code | 11 blank | 0 comment | 20 complexity | 7c75a186f87e555be8786881f72dabfe MD5 | raw file
  1. using BaconographyPortable.Services;
  2. using Microsoft.Practices.ServiceLocation;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Data;
  11. using System.Windows.Media;
  12. using Windows.UI;
  13. namespace BaconographyWP8.Converters
  14. {
  15. public class DepthColorConverter : IValueConverter
  16. {
  17. static SolidColorBrush transparent = new SolidColorBrush(System.Windows.Media.Colors.Transparent);
  18. static List<SolidColorBrush> depthBrushes = new List<SolidColorBrush>();
  19. static SolidColorBrush accentBrush = null;
  20. static ISettingsService _settingsService;
  21. private void PopulateBrushes()
  22. {
  23. var currentAccentBrush = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
  24. if (accentBrush != null && currentAccentBrush.Color == accentBrush.Color)
  25. return;
  26. depthBrushes.Clear();
  27. depthBrushes.Add(new SolidColorBrush(System.Windows.Media.Colors.Transparent));
  28. for (double i = 0.2; i <= 2.0; i+= 0.2)
  29. {
  30. if (i < 1)
  31. {
  32. int r = currentAccentBrush.Color.R;
  33. r = (int)(r == 0 ? 10 * i : r * i);
  34. r = r > 255 ? 255 : r;
  35. int g = currentAccentBrush.Color.G;
  36. g = (int)(g == 0 ? 10 * i : g * i);
  37. g = g > 255 ? 255 : g;
  38. int b = currentAccentBrush.Color.B;
  39. b = (int)(b == 0 ? 10 * i : b * i);
  40. b = b > 255 ? 255 : b;
  41. depthBrushes.Add(new SolidColorBrush(System.Windows.Media.Color.FromArgb((byte)255, (byte)r, (byte)g, (byte)b)));
  42. }
  43. else if (i == 1)
  44. {
  45. depthBrushes.Add(currentAccentBrush);
  46. }
  47. else
  48. {
  49. double r = (255 - currentAccentBrush.Color.R);
  50. r = r == 0 ? 10 : r;
  51. r *= (i - 1);
  52. r += currentAccentBrush.Color.R;
  53. r = r > 255 ? 255 : r;
  54. double g = (255 - currentAccentBrush.Color.G);
  55. g = g == 0 ? 10 : g;
  56. g *= (i - 1);
  57. g += currentAccentBrush.Color.G;
  58. g = g > 255 ? 255 : g;
  59. double b = (255 - currentAccentBrush.Color.B);
  60. b = b == 0 ? 10 : b;
  61. b *= (i - 1);
  62. b += currentAccentBrush.Color.B;
  63. b = b > 255 ? 255 : b;
  64. depthBrushes.Add(new SolidColorBrush(System.Windows.Media.Color.FromArgb((byte)255, (byte)r, (byte)g, (byte)b)));
  65. }
  66. }
  67. accentBrush = currentAccentBrush;
  68. }
  69. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  70. {
  71. if(_settingsService == null)
  72. _settingsService = ServiceLocator.Current.GetInstance<ISettingsService>();
  73. if (_settingsService.MultiColorCommentMargins)
  74. {
  75. int depth = (int)value;
  76. switch (depth)
  77. {
  78. case 0:
  79. return zero;
  80. case 1:
  81. return one;
  82. case 2:
  83. return two;
  84. case 3:
  85. return three;
  86. case 4:
  87. return four;
  88. case 5:
  89. return five;
  90. case 6:
  91. return six;
  92. case 7:
  93. return seven;
  94. case 8:
  95. return eight;
  96. case 9:
  97. return nine;
  98. case 10:
  99. return ten;
  100. default:
  101. return zero;
  102. }
  103. }
  104. else
  105. {
  106. PopulateBrushes();
  107. int depth = (int)value;
  108. switch (depth)
  109. {
  110. case 1:
  111. case 2:
  112. case 3:
  113. case 4:
  114. case 5:
  115. case 6:
  116. case 7:
  117. case 8:
  118. case 9:
  119. case 10:
  120. return depthBrushes[depth];
  121. default:
  122. return depthBrushes[0];
  123. }
  124. }
  125. }
  126. static SolidColorBrush zero = new SolidColorBrush(System.Windows.Media.Colors.Gray);
  127. static SolidColorBrush one = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 98, 170, 42));
  128. static SolidColorBrush two = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 172, 43, 80));
  129. static SolidColorBrush three = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 191, 84, 48));
  130. static SolidColorBrush four = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 64, 147, 00));
  131. static SolidColorBrush five = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 149, 00, 43));
  132. static SolidColorBrush six = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 166, 42, 00));
  133. static SolidColorBrush seven = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 0, 115, 60));
  134. static SolidColorBrush eight = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 33, 133, 85));
  135. static SolidColorBrush nine = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 150, 64));
  136. static SolidColorBrush ten = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 150, 64));
  137. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. }
  142. }