Generate distinctly different RGB colors in graphs

前端 未结 12 1229
故里飘歌
故里飘歌 2020-11-30 16:51

When generating graphs and showing different sets of data it usually a good idea to difference the sets by color. So one line is red and the next is green and so on. The pro

12条回答
  •  眼角桃花
    2020-11-30 17:25

    I implemented this algorithm in a shorter way

    void ColorValue::SetColorValue( double r, double g, double b, ColorType myType )
    {
       this->c[0] = r;
       this->c[1] = g;
       this->c[2] = b;
    
       this->type = myType;
    }
    
    
    DistinctColorGenerator::DistinctColorGenerator()
    {
       mFactor = 255;
       mColorsGenerated = 0;
       mpColorCycle = new ColorValue[6];
       mpColorCycle[0].SetColorValue( 1.0, 0.0, 0.0, TYPE_RGB);
       mpColorCycle[1].SetColorValue( 0.0, 1.0, 0.0, TYPE_RGB);
       mpColorCycle[2].SetColorValue( 0.0, 0.0, 1.0, TYPE_RGB);
       mpColorCycle[3].SetColorValue( 1.0, 1.0, 0.0, TYPE_RGB);
       mpColorCycle[4].SetColorValue( 1.0, 0.0, 1.0, TYPE_RGB);
       mpColorCycle[5].SetColorValue( 0.0, 1.0, 1.0, TYPE_RGB);
    }
    
    //----------------------------------------------------------
    
    ColorValue DistinctColorGenerator::GenerateNewColor()
    {
       int innerCycleNr = mColorsGenerated % 6;
       int outerCycleNr = mColorsGenerated / 6;
       int cycleSize = pow( 2, (int)(log((double)(outerCycleNr)) / log( 2.0 ) ) );
       int insideCycleCounter = outerCycleNr % cyclesize;
    
       if ( outerCycleNr == 0)
       {
          mFactor = 255;
       }
       else
       {
          mFactor = ( 256 / ( 2 * cycleSize ) ) + ( insideCycleCounter * ( 256 / cycleSize ) );
       }
    
       ColorValue newColor = mpColorCycle[innerCycleNr] * mFactor;
    
       mColorsGenerated++;
       return newColor;
    }
    

提交回复
热议问题