Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

値の大きさをサーモグラフィのような色(値が低い順に 青→水色→緑→黄色→赤 となるような色)に変換するには、色相環(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

krsak
仕事でC++(Qt)、趣味でC#
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした