Hello JUCE community
im creating a level meter and i want the portion of the bar above the 0 dB point to turn red. I’ve been trying to get this to work but it’s not doing exactly what I want, I was wondering how other people do this. Here is my level meter class :
class meter : public Component
{
public:
meter() : maxTruePeak(-60.0f)
{
}
void paint(Graphics& g) override
{
auto bounds = getLocalBounds().toFloat();
// Fill the entire rectangle with the background color
g.setColour(Colour(uint8(67), 47, 49, 100.f));
g.fillRoundedRectangle(bounds, 1.f);
// Check if maxTruePeak is above 0 dB
if (maxTruePeak > 0.0f)
{
// Calculate the height of the red portion based on maxTruePeak
const auto redHeight = jmap(maxTruePeak, 0.f, 6.f, 0.f, static_cast<float>(getHeight()));
// Set the color to red for the portion above 0 dB
g.setColour(Colours::red);
// Fill the rectangle with red color for the portion above 0 dB
g.fillRoundedRectangle(bounds.removeFromTop(redHeight), 1.f);
}
// Set the color for the portion below 0 dB
g.setColour(Colour(uint8(235), 80, 42, 1.f));
// Calculate the height of the non-red portion based on maxTruePeak
const auto scaledY = jmap(maxTruePeak, -60.f, 0.f, 0.f, static_cast<float>(getHeight()));
// Fill the rectangle with color for the non-red portion
g.fillRoundedRectangle(bounds.removeFromBottom(scaledY), 1.f);
}
private:
float maxTruePeak;
};
