Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color enhance #104

Merged
merged 12 commits into from
Oct 20, 2018
Merged

Conversation

gbm19
Copy link
Contributor

@gbm19 gbm19 commented Oct 9, 2018

This PR systematizes the use of ColorButton to define all colors in graphics.
It also allows to choose an alpha value (transparency/opacity) for each color.
Color values (including alpha) are stored in .sciproj files as #AARRGGBB.
Thus, files saved with this version can not be properly read with previous SciDAVis versions that stored colors as #RRGGBB.
It should solve github issue #64, and sf #278.

{
QColorDialog::setStandardColor(6*i+j, colors[i+8*j].rgb());
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers 8 and 3 bother me a bit. I assume that maybe the dialog is perhaps 8x3 or something? At very least, there should be a static_assert that 8*3 <= colors_count.

Also what happens to standard colours 3,4,5, 9,10,11, ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QColorDialog has 48 basic colors in a 6x8 grid.
Using:
for (int i=0; i < colors_count; i++) {QColorDialog::setStandardColor(i, colors[i].rgb());};
arranges the colors in columns of 6. I prefer to arrange them in rows of 8 colors each

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment along the lines of rearranging the stand ard layout of standard colours might be useful. It puzzled me why you were only allocating half the colours.

The static_assert might also be useful, in case someone decides to remove some from the list, or monkey around with the magic numbers.

Copy link
Owner

@highperformancecoder highperformancecoder Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still keen on the static_assert:

static_assert(8*6 <= colours_count, "not enough default colors");

Better still, name the 8 and the 6, and use the names both in the loop and the assert. That way, as soon as anyone touches this in an invalid way, it will thow up a compiler error.

bool ColorButton::isValidColor(const QColor& c)
{
const QColor *ite = std::find(std::begin(colors), std::end(colors), c);
return (ite->isValid());
Copy link
Owner

@highperformancecoder highperformancecoder Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dereferencing the end of a sequence (as what happens if the colour is not found) has undefined behaviour, and could even cause a null dereference crash.

The return test could be as simple as ite-color < colors_count, or to be more std libraryish ite!=std::end(colors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qt takes care of all that trouble.

Copy link
Owner

@highperformancecoder highperformancecoder Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. std::find returns an iterator pointing to the end of the sequence, which in this case will be a pointer to one past the end the colour array. ite->isValid() is completely undefined - the compiler could initialise the memory past the end of the array to zero, or some garbage pattern, or it may put a properly initialised default QColor, or it may remain uninitialised to some random value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Qt appends a properly initialised QColor(Invalid) at end of array at the constructor, because that is what I get when I test the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not Qt. It's your compiler. Someone else's compiler may do it differently. Next year, your compiler may do it differently. It is not good to rely on it.


boxCanvasColor->setColor ( c ) ;

QColor color = c;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rename the argument color?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If color needs to be mutable, which is the likely reason, why not make the argument a QColor, and pass by value?

Copy link
Owner

@highperformancecoder highperformancecoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per code review comments, there a few niggles to pick.

@@ -70,11 +95,14 @@ ColorButton::ColorButton(QWidget *parent) : QWidget(parent)

void ColorButton::init()
{
// QColorDialog has 48 basic colors in a 6x8 grid. Using:
// for (int i=0; i < colors_count; i++) {QColorDialog::setStandardColor(i, colors[i].rgb());};
// arranges the colors in columns of 6. Instead arrange them in rows of 8 colors each.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment is still slightly confusing - given a 6x8 grid, columns of 6 is the same thing as rows of 8.
You could simply say "transpose the 6x8 grid", which is what you're actually doing.

@gbm19
Copy link
Contributor Author

gbm19 commented Oct 12, 2018

Notice that last commit modifies the format of saved settings file (SciDAVis.conf)

@highperformancecoder
Copy link
Owner

Re SciDAVis.conf, is the software backwardly compatible with the previous .conf file?

@gbm19
Copy link
Contributor Author

gbm19 commented Oct 13, 2018

No. Code in this PR does not properly read color values in previous version .conf file.
And code in fc70ebc does not read .conf files written with this PR.
Until this PR there were 19 color values saved in SciDAVis.conf, 10 saved in "#rrggbb" format, and 9 saved in QVariant format.
This PR stores all color values in "#aarrggbb" format.

Correction:
This PR reads old .conf files correctly (thanks to QVariant.toString knowing it is dealing with color).
However, using the 1.23.x versions with new .conf files corrupts the settings file.

@highperformancecoder highperformancecoder merged commit c24551e into highperformancecoder:master Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants