Logo Search packages:      
Sourcecode: libpodofo version File versions  Download package

PdfColor PoDoFo::PdfColor::FromString ( const char *  pszName ) [static, inherited]

Creates a color object from a string.

Parameters:
pszNamea string describing a color.

Supported values are:

  • single gray values as string (e.g. '0.5')
  • a named color (e.g. 'auquamarine' or 'magenta')
  • hex values (e.g. FF002A (RGB) or FF12AB3D (CMYK))
  • PdfArray's
Returns:
a PdfColor object

Definition at line 722 of file PdfColor.cpp.

References PoDoFo::ePdfError_CannotConvertColor, PoDoFo::PdfColor::FromArray(), PoDoFo::PdfVariant::GetArray(), PoDoFo::PdfTokenizer::GetHexValue(), PoDoFo::PdfTokenizer::GetNextVariant(), PoDoFo::PdfTokenizer::HEX_NOT_FOUND, PoDoFo::PdfVariant::IsArray(), PoDoFo::PdfColor::PdfColor(), PoDoFo::PdfLocaleImbue(), and PODOFO_RAISE_ERROR.

{
    if( pszName ) 
    {
        size_t lLen = strlen( pszName );

        // first see if it's a single number - if so, that's a single gray value
        if( isdigit( pszName[0] ) || (pszName[0] == '.') ) 
        {
            double dGrayVal = 0.0;

            std::istringstream stream( pszName );
            PdfLocaleImbue(stream);

            if( !(stream >> dGrayVal) ) 
            {
                PODOFO_RAISE_ERROR( ePdfError_CannotConvertColor );
            }            
            else
            {
                return PdfColor( dGrayVal );
            }
        }
        // now check for a hex value (#xxxxxx or #xxxxxxxx)
        else if( pszName[0] == '#' )
        {
            ++pszName;
            if( lLen == 7 ) // RGB
            {
                unsigned int r = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1) );
                pszName += 2;
                
                unsigned int g = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1) );
                pszName += 2;

                unsigned int b = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1) );

                if ( 
                    (r != PdfTokenizer::HEX_NOT_FOUND) && 
                    (g != PdfTokenizer::HEX_NOT_FOUND) && 
                    (b != PdfTokenizer::HEX_NOT_FOUND)
                    )
                {
                    return PdfColor( static_cast<double>(r)/255.0, 
                                     static_cast<double>(g)/255.0, 
                                     static_cast<double>(b)/255.0 );
                }
                else
                {
                    PODOFO_RAISE_ERROR( ePdfError_CannotConvertColor );
                }
            }
            else if( lLen == 9 ) // CMYK
            {
                unsigned int c = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1 ) );
                pszName += 2;
                unsigned int m = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1 ) );
                pszName += 2;
                unsigned int y = (PdfTokenizer::GetHexValue( *pszName ) << 4) | 
                    PdfTokenizer::GetHexValue( *(pszName+1) );
                pszName += 2;
                unsigned int k = (PdfTokenizer::GetHexValue( *pszName ) << 4) |
                    PdfTokenizer::GetHexValue( *(pszName+1) );

                if ( 
                    (c != PdfTokenizer::HEX_NOT_FOUND) && 
                    (m != PdfTokenizer::HEX_NOT_FOUND) && 
                    (y != PdfTokenizer::HEX_NOT_FOUND) && 
                    (k != PdfTokenizer::HEX_NOT_FOUND)
                    )
                {
                    return PdfColor( static_cast<double>(c)/255.0, 
                                     static_cast<double>(m)/255.0, 
                                     static_cast<double>(y)/255.0,
                                     static_cast<double>(k)/255.0 );
                }
                else
                {
                    PODOFO_RAISE_ERROR( ePdfError_CannotConvertColor );
                }
            }
            
        }
        // PdfArray 
        else if( pszName[0] == '[' ) 
        {
            PdfTokenizer tokenizer( pszName, lLen );
            PdfVariant   var;

            tokenizer.GetNextVariant( var, NULL ); // No encryption...
            if( var.IsArray() )
                return PdfColor::FromArray( var.GetArray() );
        }
        // it must be a named RGB color
        else
        {
            std::pair<const PdfNamedColor*, const PdfNamedColor*> iterators = 
                std::equal_range( &(s_NamedColors[0]), 
                                  s_NamedColors + s_nNumNamedColors, 
                                  PdfNamedColor( pszName, PdfColor() ), NamedColorComparatorPredicate() );
            
            if( iterators.first != iterators.second )
            {
                return (*(iterators.first)).GetColor();
            }

        }
    }

    return PdfColor();
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index