Thread: Writing Background RGB to file

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    26

    Writing Background RGB to file

    In my program the user can change the background color. I'm trying to write the RGB information to the log file, but I have no Idea how to write it so i can load it later when the application is ran again. I am using the stdio.h functions to access the logfile.

    Please Help. Thanks in advance..

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Look in to fread, fwrite, fopen, fclose, and the FILE* data type.

  3. #3
    is your problem
    * you don't know how to read/write from/to files in general or
    * just how to store the RGB value?

  4. #4
    i remembered i once wrote RGB-to-string and string-to-RGB code
    for my ColorPicker EmEditor Plugin. full source can be found
    on my website, the RGB part is attached. i hope that helps.


    Code:
    //////////////////////////////////////////////////////////////
    ///  TCHARs to RGB
    //////////////////////////////////////////////////////////////
    
    
    COLORREF incol = 0;
    bool bIncolValid = true;
    for( UINT u = 0; u < 6; u++ )
    {
    	TCHAR ch = *( in + u );
    
    	if(
    		( ch >= _T( '0' ) ) &&
    		( ch <= _T( '9' ) )
    	)
    	{
    		incol +=	( ch - _T( '0' ) +  0 )
    						<< ( ( ( ( u >> 1 ) << 1 ) + ( ( u + 1 ) & 1 ) ) << 2 );
    	}
    	else if(
    		( ch >= _T( 'A' ) ) &&
    		( ch <= _T( 'F' ) )
    	)
    	{
    		incol +=	( ch - _T( 'A' ) + 10 )
    						<< ( ( ( ( u >> 1 ) << 1 ) + ( ( u + 1 ) & 1 ) ) << 2 );
    	}
    	else if(
    		( ch >= _T( 'a' ) ) &&
    		( ch <= _T( 'f' ) )
    	)
    	{
    		incol +=	( ch - _T( 'a' ) + 10 )
    						<< ( ( ( ( u >> 1 ) << 1 ) + ( ( u + 1 ) & 1 ) ) << 2 );
    	}
    	else
    	{
    		bIncolValid = false;
    
    		break;
    	}
    }

    Code:
    //////////////////////////////////////////////////////////////
    ///  RGB to TCHARs
    //////////////////////////////////////////////////////////////
    
    
    // 0x00bbggrr
    TCHAR out[ 7 ];
    TCHAR red[ 3 ];
    TCHAR green[ 3 ];
    TCHAR blue[ 3 ];
    
    _itot( GetRValue( cc.rgbResult ), red, 16 );
    _itot( GetGValue( cc.rgbResult ), green, 16 );
    _itot( GetBValue( cc.rgbResult ), blue, 16 );
    
    
    // RED
    if( red[ 1 ] == _T( '\0' ) )
    {
    	out[ 0 ] = _T( '0' );
    	out[ 1 ] = red[ 0 ];
    }
    else
    {
    	out[ 0 ] = red[ 0 ];
    	out[ 1 ] = red[ 1 ];
    }
    
    
    // GREEN
    if( green[ 1 ] == _T( '\0' ) )
    {
    	out[ 2 ] = _T( '0' );
    	out[ 3 ] = green[ 0 ];
    }
    else
    {
    	out[ 2 ] = green[ 0 ];
    	out[ 3 ] = green[ 1 ];
    }
    
    
    // BLUE
    if( blue[ 1 ] == _T( '\0' ) )
    {
    	out[ 4 ] = _T( '0' );
    	out[ 5 ] = blue[ 0 ];
    }
    else
    {
    	out[ 4 ] = blue[ 0 ];
    	out[ 5 ] = blue[ 1 ];
    }
    
    
    out[ 6 ] = _T( '\0' );

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    Quote Originally Posted by jverkoey
    Look in to fread, fwrite, fopen, fclose, and the FILE* data type.
    Well I kne wthat already
    Thanks for replying guys. I figured It out on my own after jverkoey posted. I made a small system for it.

    Writing the values:
    Code:
    FILE *LogFile;
    LogFile = fopen("Color.cfg","w");
    fputc(GetRValue(cc.rgbResult),LogFile);
    fprintf(LogFile,"\n");
    fputc(GetGValue(cc.rgbResult),LogFile);
    fprintf(LogFile,"\n");
    fputc(GetBValue(cc.rgbResult),LogFile);
    fclose(LogFile);
    Loading the values:
    Code:
    if(fopen("Color.cfg","r") != 0)
    {      
            FILE *LogFile;
            LogFile = fopen("Color.cfg","r");
            int rv,gv,bv;
            rv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            gv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            bv = fgetc(LogFile);
            wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(rv,gv,bv));
    }

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    shouldnt:

    Code:
    if(fopen("Color.cfg","r") != 0)
    {      
            FILE *LogFile;
            LogFile = fopen("Color.cfg","r");
            int rv,gv,bv;
            rv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            gv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            bv = fgetc(LogFile);
            wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(rv,gv,bv));
    }
    be:

    Code:
    FILE *LogFile;
    LogFile = fopen("Color.cfg","r");
    
    if(LogFile != 0)
    {      
            int rv,gv,bv;
            rv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            gv = fgetc(LogFile);
            fscanf(LogFile,"\n");
            bv = fgetc(LogFile);
            wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(rv,gv,bv));
    }
    
    fclose(LogFile);
    Just wondering why you put two calls to fopen..

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    I have other "If" statements that open the file for different operations. Some are reeding some are writing. I also get Access violations when I do it that way.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    i remembered i once wrote RGB-to-string and string-to-RGB code for my ColorPicker EmEditor Plugin. full source can be found on my website, the RGB part is attached. i hope that helps.
    How about:
    Code:
    //////////////////////////////////////////////////////////////
    ///  RGB to TCHARs
    //////////////////////////////////////////////////////////////
    
    _stprintf(out, _T("%02X%02X%02X"), GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
    
    //////////////////////////////////////////////////////////////
    ///  TCHARs to RGB
    //////////////////////////////////////////////////////////////
    
    int r, g, b;
    if (3 == _stscanf(in, _T("%2x%2x%2x"), &r, &g, &b))
    {
        col = RGB(r, g, b);
    }
    else
    {
        valid = false;
    }

  9. #9
    if that code worx - i guess so - i wasted a lot if time. :-(

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM