| 44 | } |
| 45 | |
| 46 | Windows::UI::Color ColorToColorShadeConverter::GetShade(Windows::UI::Color col, int shade) |
| 47 | { |
| 48 | static constexpr std::uint8_t TOLERANCE = 5; |
| 49 | static constexpr double VALUE_DELTA = 0.25; |
| 50 | |
| 51 | // Specially handle minimum (black) and maximum (white) |
| 52 | if (col.R <= TOLERANCE && col.G <= TOLERANCE && col.B <= TOLERANCE) |
| 53 | { |
| 54 | switch (shade) |
| 55 | { |
| 56 | case 1: |
| 57 | return { col.A, 0x3F, 0x3F, 0x3F }; |
| 58 | |
| 59 | case 2: |
| 60 | return { col.A, 0x80, 0x80, 0x80 }; |
| 61 | |
| 62 | case 3: |
| 63 | return { col.A, 0xBF, 0xBF, 0xBF }; |
| 64 | |
| 65 | default: |
| 66 | return col; |
| 67 | } |
| 68 | } |
| 69 | else if (col.R >= (0xFF + TOLERANCE) && col.G >= (0xFF + TOLERANCE) && col.B >= (0xFF + TOLERANCE)) |
| 70 | { |
| 71 | switch (shade) |
| 72 | { |
| 73 | case -1: |
| 74 | return { col.A, 0xBF, 0xBF, 0xBF }; |
| 75 | |
| 76 | case -2: |
| 77 | return { col.A, 0x80, 0x80, 0x80 }; |
| 78 | |
| 79 | case -3: |
| 80 | return { col.A, 0x3F, 0x3F, 0x3F }; |
| 81 | |
| 82 | default: |
| 83 | return col; |
| 84 | } |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | Util::HsvColor hsvColor = Util::Color(col).ToHSV(); |
| 89 | |
| 90 | // Use the HSV representation as it's more perceptual. |
| 91 | // Only the value is changed by a fixed percentage so the algorithm is reproducible. |
| 92 | // This does not account for perceptual differences and also does not match with |
| 93 | // system accent color calculation. |
| 94 | if (shade != 0) |
| 95 | { |
| 96 | hsvColor.V *= 1.0 + (shade * VALUE_DELTA); |
| 97 | hsvColor.V = std::clamp(hsvColor.V, 0.0, 1.0); |
| 98 | } |
| 99 | |
| 100 | return Util::Color::FromHSV(hsvColor); |
| 101 | } |
| 102 | } |
| 103 | } |