我正在将hsb颜色转换为rgb,因为我将java程序转换为c#。但是,在HSBColor的结构中,一旦我创建了一个对象,我似乎无法调用这些方法,如下所示:
HSBColor Struct
UsersController
天壤之别
public struct HSBColor
{
public static Color FromHSB(HSBColor hsbColor)
{
float r = hsbColor.b;
float g = hsbColor.b;
float b = hsbColor.b;
if (hsbColor.s != 0)
{
float max = hsbColor.b;
float dif = hsbColor.b * hsbColor.s / 255f;
float min = hsbColor.b - dif;
float h = hsbColor.h * 360f / 255f;
if (h < 60f)
{
r = max;
g = h * dif / 60f + min;
b = min;
}
}
***I know there are missing brackets here, only using snippets of code***
hsbcolor.FromHSB中的“FromHSB”(h,0.8f,b);下划线表示错误:
错误3'Fractal.Form1.HSBColor'不包含'hsbColor'的定义,并且没有可以找到接受“Fractal_Assignment.Form1.HSBColor”类型的第一个参数的扩展方法'hsbColor'(是你吗?缺少using指令或程序集引用?)
答案 0 :(得分:1)
public static Color FromHSB(HSBColor hsbColor)
这意味着它是一个静态函数。您可以按类名访问静态函数。
hsbcolor.FromHSB(h, 0.8f, b);
hsbcolor是一个变量。将此行更改为
Color color = HSBColor.FromHSB(h, 0.8f, b);
答案 1 :(得分:0)
根据您的方法签名
public static Color FromHSB(HSBColor hsbColor)
你必须这样做:
// "static" wants class (i.e. HSBColor) not instance
// FromHSB wants one HSBColor argument: "new HSBColor(...)"
Color result = HSBColor.FromHSB(new HSBColor(h, 0.8f, b));
更好的实现是将FromHSB
从 static 转换为 instance 方法:
public struct HSBColor {
// note no "static" here
public Color ToColor() {
float r = this.b; // change "hsbColor" into "this "
float g = this.g;
...
}
所以你可以把它
Color result = (new HSBColor(h, 0.8f, b)).ToColor();