【发布时间】:2016-10-19 04:57:23
【问题描述】:
我正在 C++ 中创建一个函数,该函数根据用户输入的颜色和尺寸创建一个双色渐变 .ppm 文件。我遇到的主要问题是颜色的循环,它似乎重新开始进入图像的方式,如此处所示。
当它看起来像图像的左侧但尺寸相同时。这是我用来获取图像的代码。
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Color
{
int red;
int green;
int blue;
};
void smoosh(int rows, int cols, Color leftColor, Color rightColor, string filename);
int main()
{
int y;
int x;
Color l;
Color r;
string f;
cout << "Left Color: ";
cin >> l.red >> l.green >> l.blue;
cout << "\nRight Color: ";
cin >> r.red >> r.green >> r.blue;
cout << "\nHeight: ";
cin >> y;
cout << "\nWidth: ";
cin >> x;
cout << "\nFile Name: ";
cin >> f;
smoosh(y, x, l, r, f);
return 0;
}
void smoosh(int rows, int cols, Color leftColor, Color rightColor, string filename)
{
int maxI = 256;
ofstream fout;
fout.open(filename);
fout << "P3\n";
fout << cols << " " << rows << "\n" << maxI - 1 << "\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fout << (leftColor.red + ((j / 255.0) * (rightColor.red - leftColor.red))) << " ";
fout << (leftColor.green + ((j / 255.0) * (rightColor.green - leftColor.green))) << " ";
fout << (leftColor.blue + ((j / 255.0) * (rightColor.blue - leftColor.blue)))<< " ";
}
fout << endl;
}
fout.close();
}
上图的用户输入值为 左侧颜色:255 0 0 正确颜色:255 255 0 身高:200 宽度:400,提前感谢您的帮助。
【问题讨论】:
-
j 是列号。您将它除以 255.0,这可能是最大颜色值。这是什么物理意义?
标签: c++ linear-gradients