rgb hsv转换

rgb与hsv互转 c

#include <math.h>
#include "macro.h"

int rgb2hsvi(unsigned char r, unsigned char g, unsigned char b,float* res)
{
    float rm = r/255.0, gm = g/255.0, bm = b/255.0;
    float cmax = MAX(MAX(rm,gm),bm);
    float cmin = MIN(MIN(rm,gm),bm);
    float dc = cmax - cmin;

    float h;
    float s;
    float v = cmax;

    if (v == 0)
        s = 0;
    else
        s = 1.0 - cmin/v;

    if (dc == 0)
        h = 0;
    else if (cmax == rm) 
        h = 60*fmodf(((ABS(gm-bm))/dc),6);
    else if (cmax == gm) 
        h = 60 * ((bm - rm) / dc + 2);
    else if (cmax == bm)
        h = 60 * ((rm - gm) / dc + 4);
    res[0] = h;
    res[1] = s;
    res[2] = v;
}

void hsv2rgb(float* c, float* res)
{
    float h = c[0], s = c[1] , v = c[2];
    h = fmodf(ABS(h),360.0);
    float rs = h/60.0;
    float cmax = v;
    float cmin = v*(1.0-s);
    float dc = cmax - cmin;
    float rm,gm,bm;

    if( 0 <= h && h < 120){
        rm = cmax;
        bm = cmin;
        gm = rs*dc + bm;
    } else if ( 120 <= h && h < 240) {
        gm = cmax;
        rm = cmin;
        bm = (rs-2)*dc + rm;
    } else {
        bm = cmax;
        gm = cmin;
        rm = (rs-4)*dc + gm;
    }
    res[0] = rm;
    res[1] = gm;
    res[2] = bm;
}

void rgb2hsv(float* c, float* res)
{
    unsigned char r = c[0]*255;
    unsigned char g = c[1]*255;
    unsigned char b = c[2]*255;

    rgb2hsvi(r,g,b,res);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yvee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值