我将如何表示HSL和RGB颜色作为C#中的类? [英] How would I represent an HSL and an RGB color as a class in C#?

查看:513
本文介绍了我将如何表示HSL和RGB颜色作为C#中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在C#中写一个表示HSL颜色和RGB颜色的类?

How would I write a class in C# that represents an HSL color and an RGB color?

一旦我有了这个,就可以添加一个打印输出每个类的属性?我听说过 ToString(),但我不知道如何使用它。

Once I have that, is it possible to add a method that prints out each of the class attributes? I've heard about ToString(), but I'm not sure how to use it.

代码将是非常有用的,因为我是一个学生,并试图学习如何使用C#。非常感谢!

Some sample code would be very useful, as I am a student and trying to learn how to use C#. Thanks!

推荐答案

当你发布问题,让别人为你写代码时,人们会很恼火。来自新用户的许多问题似乎要求这样,您的显示也是显式地 。希望这解释了你遇到的一些负面反应,从其他用户,在downvoting,投票关闭,甚至删除您的问题。我希望这没有给你这样一个负面的经验,你被赶走了。在未来,你会发现,如果你显示一些试图帮助自己的证据,人们更愿意帮助你。向我们展示一些你写的代码(但是不工作),或者建议你自己尝试解决问题的一些可能的方法。大多数问题不是太微不足道,而不是真正的问题。我们不是一个代码编写服务,但我们仍然乐意提供帮助。

People get annoyed when you post "questions" asking others to write code for you. Lots of questions from new users seem to ask for this, and yours even does so explicitly. Hopefully that explains some of the negative reactions you've encountered from other users, either in downvoting, voting to close, or even deleting your questions. I hope this hasn't given you such a negative experience here that you're driven away. In the future, you'll find that people are much more willing to help you if you show some evidence of having tried to help yourself first. Show us some code that you've written (but isn't working), or suggest some possible ways that you've tried to solve the problem yourself. Most questions are not "too trivial", but rather not real questions. We're not a code writing service, but we're still happy to help.

这就是说,我会尽力继续实际回答问题,因为我做在评论中。如果只是因为我喜欢处理颜色和颜色空间转换的编码问题。如何在颜色空间之间进行转换的问题已经在SO上多次提出和回答。例如,I(和其他几个人)提供了一种算法(用几种不同的语言)这里

That being said, I'll try to continue actually answering the question as I did in a comment. If only because I love coding questions that deal with color and color space conversions. The question of how to convert between color spaces has already been asked and answered several times here on SO. For example, I (and several others) provided an algorithm (in several different languages) here. It should be simple to convert either of those to whatever language you choose.

对于如何写一个类来表示这些颜色空间中的值,这是很简单的非常简单。 Google搜索也会得到已经做过同样事情的人的几个结果。就个人而言,我建议创建一个结构,而不是一个类,因为它是一个非常轻量级的对象,颜色值是不可变的(意味着它们不会改变)。这是一个完美的结构,如果你想更多的解释的差异,你可以找到 问题具有已询问,并在此回答

As far as "how do I write a class to represent values in those color spaces", that's quite simple. A Google search would have also turned up several results of people who had already done the same thing. Personally, I recommend creating a structure, rather than a class, because it's a very lightweight object and color values are immutable (meaning they don't change). That's a perfect fit for a structure, and if you'd like more explanation of the difference, you can find that question has already been asked and answered here as well.

但无论您选择哪一种,最终结果看起来都非常相似。我假设如果这是一个编程类,你已经教你所需要知道的一切,以便能够自己做。意思是,您应该已经了解了私有字段(成员变量),公共属性和方法。您将从创建一个具有3个私有字段的结构开始,以表示每个颜色值;色调(H),饱和度(S)和亮度(L)。它们应该是 Integer 类型,因为单个颜色值是0到100之间的整数,或0和360.如果你关心最大精度,你可以存储它们作为类型 Decimal ,并在公开属性中处理转换为 Integer ,但是我怀疑这是必要的。无论在哪种情况下,接下来都需要创建公开属性,将您的私有字段暴露给世界。这是面向对象编程中的一个基本概念,不应该是一个新的你。 (如果是的话,你真的需要现在停下来,得到一本解释OOP的书,不要通过Go,不要收集200美元。)这些公共属性应该是 Integer ,每个颜色成分值都应该有3个,而且它们(至少如果你创建一个不可变的结构体)是只读的(只包含一个getter,而不是一个setter)。像这样:

But whichever you choose, the end result looks very similar. I assume that if this is for a programming class, you've already been taught all you need to know to be able to do this yourself. Meaning, you should have already learned about private fields (member variables), public properties, and methods. You'll start by creating a structure with 3 private fields to represent each of the color values; Hue (H), Saturation (S), and Lightness (L). They should probably each be of type Integer, because the individual color values are whole numbers between 0 and 100, or 0 and 360. If you cared about maximum accuracy, you could store them as type Decimal and handle the conversion to Integer in the public properties that expose them to the world, but I doubt this is necessary here. In either case, you'll next need to create those public properties that expose your private fields to the world. This is a basic concept in object-oriented programming, and should not be one that's new to you. (If it is, you really need to stop now and get a book that explains OOP to you. Do not pass Go, do not collect $200.) Those public properties should be of type Integer, there should be 3 of them for each of the individual color component values, and they should (at least if you're creating an immutable structure) be read-only (include only a getter, not a setter). Something like this:

/// <summary>
/// Represents an HSL color, composed of individual
/// Hue, Saturation, and Lightness attributes.
/// </summary>
public struct HSLColor
{
    private int _hue;
    private int _saturation;
    private int _lightness;

    /// <summary>
    /// The hue attribute of the color.
    /// (This is a value, in degrees, from 0 to 360.)
    /// </summary>
    public int Hue
    {
        get { return _hue; }
    }

    /// <summary>
    /// The saturation attribute of the color.
    /// (This is a percentage between 0 and 100.)
    /// </summary>
    public int Saturation
    {
        get { return _saturation; }
    }

    /// <summary>
    /// The lightness attribute of the color.
    /// (This is a percentage between 0 and 100.)
    /// </summary>
    public int Lightness
    {
        get { return _lightness; }
    }
}

最后,你说你需要重写 ToString 方法。正如你应该已经学到的,这个方法由.NET Framework中的每个类提供,因为它是从基础 Object 继承的。当然,它被标记为 virtual ,它允许你覆盖它并提供自己的实现。最难的部分是找出它的返回值应该是什么样子。您可以选择将颜色表示为三元组,显示其三个单独的组件值。无论如何,您需要使用 String.Format 方法,它允许您创建一个包含format items的字符串,该字符串将被相应对象的值替换。例如,这是我怎么做:

Finally, you say you need to override the ToString method. As you should have already learned, this method is provided by every class in the .NET Framework because it's inherited from the base Object. Of course, it's marked as virtual, which allows you to override it and provide your own implementation. The hardest part is figuring out what its return value should look like. You might choose to represent the color as a triad, showing its three individual component values. Regardless, you'll need to make use of the String.Format method, which allows you to create a string including "format items" that get replaced by the corresponding object's value. For example, here's how I do it:

/// <summary>
/// Returns the "(HH, SS, LL)" representation of this HSLColor structure.
/// </summary>
public override string ToString()
{
    return string.Format("({0}, {1}, {2})",
                         this.Hue,
                         this.Saturation,
                         this.Lightness);
}

这应该足以让你开始。我高度鼓励您不要仅仅复制并粘贴此处提供的代码作为您的作业。你不是这样学习任何东西。启动IDE,自己编写代码,并尝试一些不同的变体。确保你了解它是如何工作,我做了什么,以及如何你自己再做一次。祝你好运!

That should be enough to get you started. I highly encourage you not to just copy and paste the code provided here as your assignment. You're not learning anything this way. Fire up your IDE, write the code yourself, and try out a couple of different variations. Make sure you understand how it works, what I've done, and how you could do it again on your own. Best of luck!

这篇关于我将如何表示HSL和RGB颜色作为C#中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆