问题
How would I write a class in C# that represents an HSL color and an RGB color?
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.
Some sample code would be very useful, as I am a student and trying to learn how to use C#. Thanks!
回答1:
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.
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.
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.
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; }
}
}
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);
}
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!
来源:https://stackoverflow.com/questions/4770921/how-would-i-represent-an-hsl-and-an-rgb-color-as-a-class-in-c