Mercurial > hg > svgui
comparison layer/ColourScale.cpp @ 1070:27163db978d8 spectrogram-minor-refactor
Use a params structure
author | Chris Cannam |
---|---|
date | Thu, 23 Jun 2016 14:51:10 +0100 |
parents | 521f7e8b0559 |
children | 65b183494331 |
comparison
equal
deleted
inserted
replaced
1069:0c4734cd33c1 | 1070:27163db978d8 |
---|---|
20 | 20 |
21 #include <cmath> | 21 #include <cmath> |
22 | 22 |
23 int ColourScale::m_maxPixel = 255; | 23 int ColourScale::m_maxPixel = 255; |
24 | 24 |
25 ColourScale::ColourScale(int colourMap, | 25 ColourScale::ColourScale(Parameters parameters) : |
26 Scale scale, | 26 m_params(parameters), |
27 double minValue, | 27 m_mapper(m_params.colourMap, 1.f, double(m_maxPixel)) |
28 double maxValue, | |
29 double threshold, | |
30 double gain) : | |
31 m_mapper(colourMap, 1.f, double(m_maxPixel)), | |
32 m_scale(scale), | |
33 m_min(minValue), | |
34 m_max(maxValue), | |
35 m_threshold(threshold), | |
36 m_gain(gain) | |
37 { | 28 { |
38 if (minValue >= maxValue) { | 29 if (m_params.minValue >= m_params.maxValue) { |
39 throw std::logic_error("maxValue must be greater than minValue"); | 30 throw std::logic_error("maxValue must be greater than minValue"); |
40 } | 31 } |
41 | 32 |
42 m_mappedMin = m_min; | 33 m_mappedMin = m_params.minValue; |
43 m_mappedMax = m_max; | 34 m_mappedMax = m_params.maxValue; |
44 | 35 |
45 if (m_scale == LogColourScale) { | 36 if (m_params.scale == LogColourScale) { |
46 | 37 |
47 LogRange::mapRange(m_mappedMin, m_mappedMax); | 38 LogRange::mapRange(m_mappedMin, m_mappedMax); |
48 | 39 |
49 } else if (m_scale == PlusMinusOneScale) { | 40 } else if (m_params.scale == PlusMinusOneScale) { |
50 | 41 |
51 m_mappedMin = -1.0; | 42 m_mappedMin = -1.0; |
52 m_mappedMax = 1.0; | 43 m_mappedMax = 1.0; |
53 | 44 |
54 } else if (m_scale == AbsoluteScale) { | 45 } else if (m_params.scale == AbsoluteScale) { |
55 | 46 |
56 m_mappedMin = fabs(m_mappedMin); | 47 m_mappedMin = fabs(m_mappedMin); |
57 m_mappedMax = fabs(m_mappedMax); | 48 m_mappedMax = fabs(m_mappedMax); |
58 if (m_mappedMin >= m_mappedMax) { | 49 if (m_mappedMin >= m_mappedMax) { |
59 std::swap(m_mappedMin, m_mappedMax); | 50 std::swap(m_mappedMin, m_mappedMax); |
68 int | 59 int |
69 ColourScale::getPixel(double value) | 60 ColourScale::getPixel(double value) |
70 { | 61 { |
71 double maxPixF = m_maxPixel; | 62 double maxPixF = m_maxPixel; |
72 | 63 |
73 if (m_scale == PhaseColourScale) { | 64 if (m_params.scale == PhaseColourScale) { |
74 double half = (maxPixF - 1.f) / 2.f; | 65 double half = (maxPixF - 1.f) / 2.f; |
75 return 1 + int((value * half) / M_PI + half); | 66 return 1 + int((value * half) / M_PI + half); |
76 } | 67 } |
77 | 68 |
78 value *= m_gain; | 69 value *= m_params.gain; |
79 | 70 |
80 if (value < m_threshold) return 0; | 71 if (value < m_params.threshold) return 0; |
81 | 72 |
82 double mapped = value; | 73 double mapped = value; |
83 | 74 |
84 if (m_scale == LogColourScale) { | 75 if (m_params.scale == LogColourScale) { |
85 mapped = LogRange::map(value); | 76 mapped = LogRange::map(value); |
86 } else if (m_scale == PlusMinusOneScale) { | 77 } else if (m_params.scale == PlusMinusOneScale) { |
87 if (mapped < -1.f) mapped = -1.f; | 78 if (mapped < -1.f) mapped = -1.f; |
88 if (mapped > 1.f) mapped = 1.f; | 79 if (mapped > 1.f) mapped = 1.f; |
89 } else if (m_scale == AbsoluteScale) { | 80 } else if (m_params.scale == AbsoluteScale) { |
90 if (mapped < 0.f) mapped = -mapped; | 81 if (mapped < 0.f) mapped = -mapped; |
91 } | 82 } |
92 | 83 |
93 if (mapped < m_mappedMin) { | 84 if (mapped < m_mappedMin) { |
94 mapped = m_mappedMin; | 85 mapped = m_mappedMin; |
99 | 90 |
100 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin); | 91 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin); |
101 | 92 |
102 int pixel = 0; | 93 int pixel = 0; |
103 | 94 |
104 if (m_scale == MeterColourScale) { | 95 if (m_params.scale == MeterColourScale) { |
105 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1; | 96 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1; |
106 } else { | 97 } else { |
107 pixel = int(proportion * maxPixF) + 1; | 98 pixel = int(proportion * maxPixF) + 1; |
108 } | 99 } |
109 | 100 |
110 if (pixel > m_maxPixel) pixel = m_maxPixel; | 101 if (pixel < 0) { |
111 if (pixel < 0) pixel = 0; | 102 pixel = 0; |
103 } | |
104 if (pixel > m_maxPixel) { | |
105 pixel = m_maxPixel; | |
106 } | |
112 return pixel; | 107 return pixel; |
113 } | 108 } |
114 | 109 |
115 QColor | 110 QColor |
116 ColourScale::getColourForPixel(int pixel, int rotation) | 111 ColourScale::getColourForPixel(int pixel, int rotation) |