| 945 | } |
| 946 | |
| 947 | void ColorPicker::UpdateColorControlValues() |
| 948 | { |
| 949 | if (m_IsInitialized) |
| 950 | { |
| 951 | bool eventsDisconnectedByMethod = false; |
| 952 | Util::Color rgbColor = Color(); |
| 953 | |
| 954 | // Disable events during the update |
| 955 | if (m_EventsConnected) |
| 956 | { |
| 957 | ConnectEvents(false); |
| 958 | eventsDisconnectedByMethod = true; |
| 959 | } |
| 960 | |
| 961 | if (m_HexInputTextBox) |
| 962 | { |
| 963 | uint32_t colorRgba = rgbColor.ToRGBA(); |
| 964 | |
| 965 | hstring colorHex; |
| 966 | if (IsAlphaEnabled()) |
| 967 | { |
| 968 | colorHex = winrt::format(L"{:08X}", colorRgba); |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | colorHex = winrt::format(L"{:06X}", static_cast<uint32_t>(colorRgba >> 8)); |
| 973 | } |
| 974 | |
| 975 | m_HexInputTextBox.Text(colorHex); |
| 976 | } |
| 977 | |
| 978 | // Regardless of the active color representation, the spectrum is always HSV |
| 979 | // Therefore, always calculate HSV color here |
| 980 | // Warning: Always maintain/use HSV information in the saved HSV color |
| 981 | // This avoids loss of precision and drift caused by continuously converting to/from RGB |
| 982 | if (!m_SavedHsvColor) |
| 983 | { |
| 984 | if (m_UpdateFromSpectrum && m_ColorSpectrumControl) |
| 985 | { |
| 986 | // grab it from the spectrum for more precision |
| 987 | m_SavedHsvColor = Util::HsvColor(m_ColorSpectrumControl.HsvColor()); |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | auto hsvCol = rgbColor.ToHSV(); |
| 992 | |
| 993 | // Round the channels, be sure rounding matches with the scaling next |
| 994 | // Rounding of SVA requires at MINIMUM 2 decimal places |
| 995 | hsvCol.H = std::round(hsvCol.H); |
| 996 | hsvCol.S = std::floor(hsvCol.S * 100.0) / 100.0; |
| 997 | hsvCol.V = std::floor(hsvCol.V * 100.0) / 100.0; |
| 998 | hsvCol.A = std::floor(hsvCol.A * 100.0) / 100.0; |
| 999 | |
| 1000 | // Must update HSV color |
| 1001 | m_SavedHsvColor = hsvCol; |
| 1002 | } |
| 1003 | |
| 1004 | m_SavedHsvColorRgbEquivalent = rgbColor; |