Fix wxColour::GetAsString called for non-solid colour
Don't return RGB values if colour is not solid and hence cannot be represented with these values. Non-solid colour should be reported as an unknown RGB value, e.g. '??????'. Closes #18596.
This commit is contained in:
@@ -118,6 +118,9 @@ public:
|
|||||||
@c wxC2S_CSS_SYNTAX (which is the only one supporting alpha) is not
|
@c wxC2S_CSS_SYNTAX (which is the only one supporting alpha) is not
|
||||||
specified in flags.
|
specified in flags.
|
||||||
|
|
||||||
|
@note For non-solid (i.e. non-RGB) colour this function returns
|
||||||
|
"rgb(??, ?? ??)" or "#??????".
|
||||||
|
|
||||||
@since 2.7.0
|
@since 2.7.0
|
||||||
*/
|
*/
|
||||||
virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const;
|
virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const;
|
||||||
|
@@ -220,44 +220,58 @@ wxString wxColourBase::GetAsString(long flags) const
|
|||||||
{
|
{
|
||||||
wxString colName;
|
wxString colName;
|
||||||
|
|
||||||
const int alpha = Alpha();
|
if ( IsSolid() )
|
||||||
const bool isOpaque = alpha == wxALPHA_OPAQUE;
|
|
||||||
|
|
||||||
// we can't use the name format if the colour is not opaque as the alpha
|
|
||||||
// information would be lost
|
|
||||||
if ( (flags & wxC2S_NAME) && isOpaque )
|
|
||||||
{
|
{
|
||||||
colName = wxTheColourDatabase->FindName(
|
const int alpha = Alpha();
|
||||||
static_cast<const wxColour &>(*this)).MakeLower();
|
const bool isOpaque = alpha == wxALPHA_OPAQUE;
|
||||||
|
|
||||||
|
// we can't use the name format if the colour is not opaque as the alpha
|
||||||
|
// information would be lost
|
||||||
|
if ( (flags & wxC2S_NAME) && isOpaque )
|
||||||
|
{
|
||||||
|
colName = wxTheColourDatabase->FindName(
|
||||||
|
static_cast<const wxColour &>(*this)).MakeLower();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( colName.empty() )
|
||||||
|
{
|
||||||
|
const int red = Red(),
|
||||||
|
green = Green(),
|
||||||
|
blue = Blue();
|
||||||
|
|
||||||
|
if ( flags & wxC2S_CSS_SYNTAX )
|
||||||
|
{
|
||||||
|
// no name for this colour; return it in CSS syntax
|
||||||
|
if ( isOpaque )
|
||||||
|
{
|
||||||
|
colName.Printf(wxT("rgb(%d, %d, %d)"), red, green, blue);
|
||||||
|
}
|
||||||
|
else // use rgba() form
|
||||||
|
{
|
||||||
|
colName.Printf(wxT("rgba(%d, %d, %d, %s)"),
|
||||||
|
red, green, blue,
|
||||||
|
wxString::FromCDouble(alpha / 255., 3));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ( flags & wxC2S_HTML_SYNTAX )
|
||||||
|
{
|
||||||
|
// no name for this colour; return it in HTML syntax
|
||||||
|
if ( isOpaque )
|
||||||
|
colName.Printf(wxT("#%02X%02X%02X"), red, green, blue);
|
||||||
|
else
|
||||||
|
colName.Printf(wxT("#%02X%02X%02X%02X"), red, green, blue, alpha);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if ( colName.empty() )
|
|
||||||
{
|
{
|
||||||
const int red = Red(),
|
|
||||||
green = Green(),
|
|
||||||
blue = Blue();
|
|
||||||
|
|
||||||
if ( flags & wxC2S_CSS_SYNTAX )
|
if ( flags & wxC2S_CSS_SYNTAX )
|
||||||
{
|
{
|
||||||
// no name for this colour; return it in CSS syntax
|
colName = wxS("rgb(??, ??, \?\?)"); // \? form to avoid ??) trigraph
|
||||||
if ( isOpaque )
|
|
||||||
{
|
|
||||||
colName.Printf(wxT("rgb(%d, %d, %d)"), red, green, blue);
|
|
||||||
}
|
|
||||||
else // use rgba() form
|
|
||||||
{
|
|
||||||
colName.Printf(wxT("rgba(%d, %d, %d, %s)"),
|
|
||||||
red, green, blue,
|
|
||||||
wxString::FromCDouble(alpha / 255., 3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if ( flags & wxC2S_HTML_SYNTAX )
|
else if ( flags & wxC2S_HTML_SYNTAX )
|
||||||
{
|
{
|
||||||
// no name for this colour; return it in HTML syntax
|
colName = wxS("#??????");
|
||||||
if ( isOpaque )
|
|
||||||
colName.Printf(wxT("#%02X%02X%02X"), red, green, blue);
|
|
||||||
else
|
|
||||||
colName.Printf(wxT("#%02X%02X%02X%02X"), red, green, blue, alpha);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user