63
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

値の大きさをサーモグラフィのような色に変換する

Posted at

値の大きさをサーモグラフィのような色(値が低い順に 青→水色→緑→黄色→赤 となるような色)に変換するには、色相環(Hue)を使います。

色見本1

colorscale.c
#include <stdio.h>
#include <math.h>

int ColorScaleBCGYR( double in_value )
{
    // 0.0~1.0 の範囲の値をサーモグラフィみたいな色にする
    // 0.0                    1.0
    // 青    水    緑    黄    赤
    // 最小値以下 = 青
    // 最大値以上 = 赤
    int ret;
    int a = 255;    // alpha値
    int r, g, b;    // RGB値
    double  value = in_value;
    double  tmp_val = cos( 4 * M_PI * value );
    int     col_val = (int)( ( -tmp_val / 2 + 0.5 ) * 255 );
         if ( value >= ( 4.0 / 4.0 ) ) { r = 255;     g = 0;       b = 0;       }   // 赤
    else if ( value >= ( 3.0 / 4.0 ) ) { r = 255;     g = col_val; b = 0;       }   // 黄~赤
    else if ( value >= ( 2.0 / 4.0 ) ) { r = col_val; g = 255;     b = 0;       }   // 緑~黄
    else if ( value >= ( 1.0 / 4.0 ) ) { r = 0;       g = 255;     b = col_val; }   // 水~緑
    else if ( value >= ( 0.0 / 4.0 ) ) { r = 0;       g = col_val; b = 255;     }   // 青~水
    else {                               r = 0;       g = 0;       b = 255;     }   // 青
    ret = (a&0x000000FF) << 24
        | (r&0x000000FF) << 16
        | (g&0x000000FF) <<  8
        | (b&0x000000FF);
    return ret;
}

int main()
{
    double value;
    double min, max;

    value = 0.00;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR(value) );
    value = 0.25;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR(value) );
    value = 0.50;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR(value) );
    value = 0.75;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR(value) );
    value = 1.00;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR(value) );

    printf( "\n" );

    // 値の範囲を変える場合(例: 25.0~40.0)
    min = 25.0;
    max = 40.0;
    printf( "min=%e, max=%e\n", min, max );
    value = 27.0;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR((value-min)/(max-min)) );
    value = 28.0;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR((value-min)/(max-min)) );
    value = 30.0;   printf( "value=%e(0x%x)\n", value, ColorScaleBCGYR((value-min)/(max-min)) );
    // わざわざ書かなくても分かるだろうけど、min=maxのときゼロ除算になるので注意

    return 0;
}

なお、このソースでは各RGB値の変化で三角関数を使っています。
各RGB値の変化を線形にした場合、次のように水色と黄色の領域が狭くなり、またグラデーションもあまり綺麗に見えません。

色見本2

63
58
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
63
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?