Anka Hanım
Anka Hanım

Reputation: 1

Brightness normalization and flickering issue in circles creating a twinkle effect with changing brightness over time

I am controlling the brightness values of the circle using an enum structure. With a timer duration, each circle should flicker at a different time with varying brightness levels. When the brightness exceeds 1, it should be clamped to 1 and transition to the 'on' state, remaining there for a certain duration. If the brightness becomes a negative value, I need a normalization equation to make it positive. In the brightness equation, while each circle has different brightness levels, the brightness should gradually decrease step by step until the timer duration stops, making the circles fade out completely. My main focus is how to normalize the brightness and gradually reduce it to create a twinkle effect.

Notes: rval and fval variables are normalized values ​​between 1.0-0.01 according to the slider value.

#include <QCoreApplication>
#include <QTimer>
#include <QRandomGenerator>
#include <QDebug>
#include <algorithm>

int main(int argc, char* argv[]) {
QCoreApplication app(argc, argv);

double brightness = 0.01; 
double r_val = 0.02;      
double f_val = 0.02;      
int minOnTime = 500;  
int maxOnTime = 1000;
enum State { Raise, Fall, On }; 
State state = Raise; 

QTimer simulationTimer; 
QTimer onTimer; 

QObject::connect(&simulationTimer,&QTimer::timeout [&](){
brightness =std::clamp(brightness,0.01, 1.0);
r_val= slider->value;
f_val= slider2->value;

if (state == Raise) {
   brightness += r_val; 
   if (brightness >= 1.0) {
       brightness = 1.0;
       state = On; 
       int duration = QRandomGenerator::global()->bounded(minOnTime, maxOnTime) + 1;
            onTimer.start(duration); 
        }
    } else if (state == Fall) {
        brightness -= f_val; 
        if (brightness <= 0.01) {
            brightness = 0.01;
            state = Raise; 
        }
    }
 Qobject::connect(&onTimer,&QTimer::timeout, [&]()
{
onTimer.stop();
state = Fall;
}

simulationTimer.start(100);

return app.exec();
}

Upvotes: -1

Views: 86

Answers (0)

Related Questions