added support for alpha channel to wxColour to/from string conversion functions (slightly modified patch 1750112)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47361 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-07-12 08:36:24 +00:00
parent 1d66b09982
commit b534968dc3
2 changed files with 77 additions and 31 deletions

View File

@@ -118,17 +118,21 @@ Returns the blue intensity.
Converts this colour to a \helpref{wxString}{wxstring} Converts this colour to a \helpref{wxString}{wxstring}
using the given {\it flags}. using the given {\it flags}.
The supported flags are {\bf wxC2S\_NAME}, to obtain the colour The supported flags are \textbf{wxC2S\_NAME}, to obtain the colour name (e.g.
name (e.g. wxColour(255,0,0) -> \texttt{``red"}), {\bf wxC2S\_CSS\_SYNTAX}, to obtain wxColour(255,0,0) -> \texttt{``red"}), \textbf{wxC2S\_CSS\_SYNTAX}, to obtain
the colour in the \texttt{``rgb(r,g,b)"} syntax the colour in the \texttt{``rgb(r,g,b)"} or \texttt{``rgba(r,g,b,a)"} syntax
(e.g. wxColour(255,0,0) -> \texttt{``rgb(255,0,0)"}), and {\bf wxC2S\_HTML\_SYNTAX}, to obtain (e.g. wxColour(255,0,0,85) -> \texttt{``rgba(255,0,0,0.333)"}), and
the colour as \texttt{``\#"} followed by 6 hexadecimal digits \textbf{wxC2S\_HTML\_SYNTAX}, to obtain the colour as \texttt{``\#"} followed
(e.g. wxColour(255,0,0) -> \texttt{``\#FF0000"}). by 6 hexadecimal digits (e.g. wxColour(255,0,0) -> \texttt{``\#FF0000"}).
This function never fails and always returns a non-empty string. This function never fails and always returns a non-empty string but asserts if
the colour has alpha channel (i.e. is non opaque) but
\textbf{wxC2S\_CSS\_SYNTAX} (which is the only one supporting alpha) is not
specified in flags.
\newsince{2.7.0} \newsince{2.7.0}
\membersection{wxColour::GetPixel}\label{wxcolourgetpixel} \membersection{wxColour::GetPixel}\label{wxcolourgetpixel}
\constfunc{long}{GetPixel}{\void} \constfunc{long}{GetPixel}{\void}
@@ -170,7 +174,11 @@ Returns the red intensity.
Sets the RGB intensity values using the given values (first overload), extracting them from the packed long (second overload), using the given string (third overloard). Sets the RGB intensity values using the given values (first overload), extracting them from the packed long (second overload), using the given string (third overloard).
When using third form, Set() accepts: colour names (those listed in \helpref{wxTheColourDatabase}{wxcolourdatabase}), the CSS-like \texttt{``RGB(r,g,b)"} syntax (case insensitive) and the HTML-like syntax (i.e. \texttt{``\#"} followed by 6 hexadecimal digits for red, green, blue components). When using third form, Set() accepts: colour names (those listed in
\helpref{wxTheColourDatabase}{wxcolourdatabase}), the CSS-like
\texttt{``rgb(r,g,b)"} or \texttt{``rgba(r,g,b,a)"} syntax (case insensitive)
and the HTML-like syntax (i.e. \texttt{``\#"} followed by 6 hexadecimal digits
for red, green, blue components).
Returns \true if the conversion was successful, \false otherwise. Returns \true if the conversion was successful, \false otherwise.

View File

@@ -39,19 +39,34 @@ bool wxColourBase::FromString(const wxString& str)
if ( str.empty() ) if ( str.empty() )
return false; // invalid or empty string return false; // invalid or empty string
if ( wxStrncmp(str, wxT("RGB"), 3) == 0 || if ( wxStrnicmp(str, wxT("RGB"), 3) == 0 )
wxStrncmp(str, wxT("rgb"), 3) == 0 )
{ {
// CSS-like RGB specification // CSS-like RGB specification
// according to http://www.w3.org/TR/REC-CSS2/syndata.html#color-units // according to http://www.w3.org/TR/css3-color/#colorunits
// values outside 0-255 range are allowed but should be clipped // values outside 0-255 range are allowed but should be clipped
int red, green, blue; int red, green, blue,
if (wxSscanf(str.wx_str() + 3, wxT("(%d, %d, %d)"), &red, &green, &blue) != 3) alpha = wxALPHA_OPAQUE;
return false; if ( str.length() > 3 && (str[3] == wxT('a') || str[3] == wxT('A')) )
{
float a;
// TODO: use locale-independent function
if ( wxSscanf(str.wx_str() + 4, wxT("( %d , %d , %d , %f )"),
&red, &green, &blue, &a) != 4 )
return false;
Set((unsigned char)wxClip(red,0,255), alpha = wxRound(a * 255);
(unsigned char)wxClip(green,0,255), }
(unsigned char)wxClip(blue,0,255)); else // no 'a' following "rgb"
{
if ( wxSscanf(str.wx_str() + 3, wxT("( %d , %d , %d )"),
&red, &green, &blue) != 3 )
return false;
}
Set((unsigned char)wxClip(red, 0, 255),
(unsigned char)wxClip(green, 0, 255),
(unsigned char)wxClip(blue, 0, 255),
(unsigned char)wxClip(alpha, 0, 255));
} }
else if ( str[0] == wxT('#') && wxStrlen(str) == 7 ) else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
{ {
@@ -88,23 +103,46 @@ wxString wxColourBase::GetAsString(long flags) const
{ {
wxString colName; wxString colName;
if (flags & wxC2S_NAME) const bool isOpaque = Alpha() == wxALPHA_OPAQUE;
colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower();
if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) ) // 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 )
{ {
// no name for this colour; return it in CSS syntax colName = wxTheColourDatabase->FindName(
colName.Printf(wxT("rgb(%d, %d, %d)"), wx_static_cast(const wxColour &, *this)).MakeLower();
Red(), Green(), Blue());
}
else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) )
{
// no name for this colour; return it in HTML syntax
colName.Printf(wxT("#%02X%02X%02X"),
Red(), Green(), Blue());
} }
// this function always returns a non-empty string if ( colName.empty() )
{
const int red = Red(),
blue = Blue(),
green = Green();
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
{
// TODO: use locale-independent function
colName.Printf(wxT("rgba(%d, %d, %d, %.3f)"),
red, green, blue, Alpha() / 255.);
}
}
else if ( flags & wxC2S_HTML_SYNTAX )
{
wxASSERT_MSG( isOpaque, "alpha is lost in HTML syntax" );
// no name for this colour; return it in HTML syntax
colName.Printf(wxT("#%02X%02X%02X"), red, green, blue);
}
}
// this function should alway returns a non-empty string
wxASSERT_MSG(!colName.empty(), wxASSERT_MSG(!colName.empty(),
wxT("Invalid wxColour -> wxString conversion flags")); wxT("Invalid wxColour -> wxString conversion flags"));