rgb转hsv代码C语音,不支持浮点运算
时间: 2024-11-12 20:39:34 浏览: 3
RGB到HSV(色调、饱和度、亮度)颜色空间转换在C语言中通常需要整数运算,因为标准库可能不直接支持浮点运算。以下是一个简单的整数RGB转HSV的C代码示例:
```c
#include <stdio.h>
// 定义RGB和HSV范围(0-255)
#define RGB_MAX 255
#define HSV_MAX 255
// RGB to HSV conversion function
void rgb_to_hsv(int r, int g, int b, int* h, int* s, int* v) {
int max, min;
int delta;
// Find the maximum and minimum values among R, G, B
if (r > g && r > b)
max = r;
else if (g > b)
max = g;
else
max = b;
min = r < g ? r : g;
min = min < b ? min : b;
*v = max; // V (Value): The brightest component of the color
if (max == min) { // If all values are equal, hue is undefined (gray)
*h = -1;
*s = 0;
} else {
delta = max - min;
if (max == r)
*h = (g - b) * (RGB_MAX / delta);
else if (max == g)
*h = 2 + (b - r) * (RGB_MAX / delta);
else
*h = 4 + (r - g) * (RGB_MAX / delta);
*h *= RGB_MAX / 6; // Normalize hue value between 0 and 255
*h %= HSV_MAX; // Ensure hue stays within [0, 255]
*s = *v * (delta != 0 ? (RGB_MAX / delta) : 0); // S (Saturation)
}
}
int main() {
int r, g, b, h, s, v;
printf("Enter RGB values (0-255): ");
scanf("%d %d %d", &r, &g, &b);
rgb_to_hsv(r, g, b, &h, &s, &v);
printf("HSV: Hue = %d, Saturation = %d, Value = %d\n", h, s, v);
return 0;
}
```
阅读全文