//! Picks random colors for foreground and backgound on the dialog box. Tries to pick contrasting colors //! so the text remains visible. void PoppyUpper::setRandomColors() { // pick random foreground/background colors (but not the same foreground and background!) // color vector has CSScolors.size() elements, so indexes are zero to size()-1 Randoid colorRandThing(0, CSSColors.size() - 1); unsigned int bgInd = colorRandThing.getRand(); QString backgroundColorStr = CSSColors[bgInd]; unsigned int textInd = colorRandThing.getRand(); QString textColorStr = CSSColors[textInd]; // Make sure they're not the same and have suitable contrast // we'll try using the lightness value from the HSL representation of the color, // and make sure they differ by at least 125 QColor bgColor(backgroundColorStr); QColor txtColor(textColorStr); while (abs(bgColor.lightness() - txtColor.lightness()) < 125) { textInd = colorRandThing.getRand(); textColorStr = CSSColors[textInd]; txtColor = QColor(textColorStr); } QPalette thePalette; thePalette.setColor(QPalette::Window, backgroundColorStr); thePalette.setColor(QPalette::WindowText, textColorStr); thePalette.setColor(QPalette::Text, textColorStr); setPalette(thePalette); }
int main(){ for (int i = 0; i < 255; ++i){ txtColor(i); std::cout << i << std::endl; } return 0; }