【发布时间】:2020-07-08 20:47:27
【问题描述】:
我想根据亮度计算 RGB 值。
我知道的数据是:
- 新的亮度(我要应用的值)
- 旧照度
- 旧的 RGB 值。
我们可以像这样从 RGB 值计算亮度:uint8_t luminance = R * 0.21 + G * 0.71 + B * 0.07;
我的代码是:
// We create a function to set the luminance of a pixel
void jpegImage::setLuminance(uint8_t newLuminance, unsigned int x, unsigned int y) {
// If the X or Y value is out of range, we throw an error
if(x >= width) {
throw std::runtime_error("Error : in jpegImage::setLuminance : The X value is out of range");
}
else if(y >= height) {
throw std::runtime_error("Error : in jpegImage::setLuminance : The Y value is out of range");
}
// If the image is monochrome
if(pixelSize == 1) {
// We set the pixel value to the luminance
pixels[y][x] = newLuminance;
}
// Else if the image is colored, we throw an error
else if(pixelSize == 3) {
// I don't know how to proceed
// My image is stored in a std::vector<std::vector<uint8_t>> pixels;
// This is a list that contain the lines of the image
// Each line contains the RGB values of the following pixels
// For example an image with 2 columns and 3 lines
// [[R, G, B, R, G, B], [R, G, B, R, G, B], [R, G, B, R, G, B]]
// For example, the R value with x = 23, y = 12 is:
// pixels[12][23 * pixelSize];
// For example, the B value with x = 23, y = 12 is:
// pixels[12][23 * pixelSize + 2];
// (If the image is colored, the pixelSize will be 3 (R, G and B)
// (If the image is monochrome the pixelSIze will be 1 (just the luminance value)
}
}
我该如何继续? 谢谢!
【问题讨论】:
-
您不能仅从亮度计算颜色。您需要三个属性,如Hue + Saturation + Luminance。您是在问“我怎样才能使颜色变亮或变暗”?
-
您应该注意,“亮度”具有精确的含义;它是到达观察者眼睛的光量,以每平方米坎德拉为单位(非正式地称为“尼特”)。这就是你所说的“亮度”吗?可能不会,因为通常“RGB 像素”没有这种亮度。此外,RGB 有很多版本,而不仅仅是 sRGB。