将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
本文介绍了将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找从 RGB 到 HSV 的颜色空间转换器,特别是对于两个颜色空间的 0 到 255 范围.

I am looking for color space converter from RGB to HSV, specifically for the range 0 to 255 for both color spaces.

推荐答案

我已经使用这些很长时间了 - 现在不知道它们来自哪里......注意输入和输出,除了角度以度为单位,范围为 0 到 1.0.

I've used these for a long time - no idea where they came from at this point... Note that the inputs and outputs, except for the angle in degrees, are in the range of 0 to 1.0.

注意:此代码不会对输入进行真正的健全性检查.谨慎行事!

NOTE: this code does no real sanity checking on inputs. Proceed with caution!

typedef struct {
    double r;       // a fraction between 0 and 1
    double g;       // a fraction between 0 and 1
    double b;       // a fraction between 0 and 1
} rgb;

typedef struct {
    double h;       // angle in degrees
    double s;       // a fraction between 0 and 1
    double v;       // a fraction between 0 and 1
} hsv;

static hsv   rgb2hsv(rgb in);
static rgb   hsv2rgb(hsv in);

hsv rgb2hsv(rgb in)
{
    hsv         out;
    double      min, max, delta;

    min = in.r < in.g ? in.r : in.g;
    min = min  < in.b ? min  : in.b;

    max = in.r > in.g ? in.r : in.g;
    max = max  > in.b ? max  : in.b;

    out.v = max;                                // v
    delta = max - min;
    if (delta < 0.00001)
    {
        out.s = 0;
        out.h = 0; // undefined, maybe nan?
        return out;
    }
    if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
        out.s = (delta / max);                  // s
    } else {
        // if max is 0, then r = g = b = 0              
        // s = 0, h is undefined
        out.s = 0.0;
        out.h = NAN;                            // its now undefined
        return out;
    }
    if( in.r >= max )                           // > is bogus, just keeps compilor happy
        out.h = ( in.g - in.b ) / delta;        // between yellow & magenta
    else
    if( in.g >= max )
        out.h = 2.0 + ( in.b - in.r ) / delta;  // between cyan & yellow
    else
        out.h = 4.0 + ( in.r - in.g ) / delta;  // between magenta & cyan

    out.h *= 60.0;                              // degrees

    if( out.h < 0.0 )
        out.h += 360.0;

    return out;
}


rgb hsv2rgb(hsv in)
{
    double      hh, p, q, t, ff;
    long        i;
    rgb         out;

    if(in.s <= 0.0) {       // < is bogus, just shuts up warnings
        out.r = in.v;
        out.g = in.v;
        out.b = in.v;
        return out;
    }
    hh = in.h;
    if(hh >= 360.0) hh = 0.0;
    hh /= 60.0;
    i = (long)hh;
    ff = hh - i;
    p = in.v * (1.0 - in.s);
    q = in.v * (1.0 - (in.s * ff));
    t = in.v * (1.0 - (in.s * (1.0 - ff)));

    switch(i) {
    case 0:
        out.r = in.v;
        out.g = t;
        out.b = p;
        break;
    case 1:
        out.r = q;
        out.g = in.v;
        out.b = p;
        break;
    case 2:
        out.r = p;
        out.g = in.v;
        out.b = t;
        break;

    case 3:
        out.r = p;
        out.g = q;
        out.b = in.v;
        break;
    case 4:
        out.r = t;
        out.g = p;
        out.b = in.v;
        break;
    case 5:
    default:
        out.r = in.v;
        out.g = p;
        out.b = q;
        break;
    }
    return out;     
}

这篇关于将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MUI Picker Input设置默认时间回显实例代码 mui('body').on('tap', '#begTime', function () { //日期选择 var dtPicker = new mui.DtPicker({ type: 'date' }); /*参数:'datetime'-完整日期视图(年月日时分) 'date'--年视图(年月日) 'time' --时间视图(时分
创建容器 xm-select-demo,选择容器 隐藏赋值取值表单; div id="selected_box" class="layui-input-inline xm-select-demo" style="width:150px;"input type="hidden" id="poi_category_id"/div 异步获取代码 //获取分类; $.ajax({ type: 'get', url: "./ap
mui消息框alert,confirm,prompt,toast实例 script type="text/javascript" charset="utf-8" //mui初始化 mui.init({ swipeBack: true //启用右滑关闭功能 }); var info = document.getElementById("info"); document.getElementById("alertBtn").addEventL
文件发送的代码如下: ?$url = 'http://127.0.0.1/fujian/jieshou.php'; // 目标URL$filePath2 = iconv("utf-8","gb2312",'D:/MYOA/webroot/fujian/1/副本.txt'); // 文件路$filePath1 = iconv("utf-8","gb2312",'D:/MYOA/webroot/fujian/1/1.txt'); // 文件
PHP对Excel导入导出操作 最近公司要做报表功能,各种财务报表、工资报表、考勤报表等,于是特地封装适用各大场景的导入导出操作,希望大神支出不足之处。 phpspreadsheet 引入 由于PHPExcel早就停止更新维护,所以适用phpspreadsheet。不知道如何通过composer
使用PhpOffice\PhpSpreadsheet 实现Excel多sheet导出代码,具体代码如下: /** * 多sheet的导出 * @author bwy xxx@xxx.com * @param [type] $data_array * @return void */ public function xtexport($data_array) { $name = '团长' . date("Y-m-d", time())