angularsen / UnitsNet

Makes life working with units of measurement just a little bit better.
https://www.nuget.org/packages/UnitsNet/
MIT No Attribution
2.19k stars 380 forks source link

Pixel Quantity #1078

Closed ptasev closed 2 years ago

ptasev commented 2 years ago

Is your feature request related to a problem? Please describe. I'm working on a project that often uses pixel values. It would be nice to have a unit for this so that it's possible to specify "millipixel", "micropixel" etc.

Describe the solution you'd like Add a new Pixel unit. I'm not sure what the actual design should look like. The problem is a little challenging because technically pixel is a measure of distance so it could be considered "Length", but there's no standard conversion between meter and pixel. So that makes me think it's better to have a separate unit. Not, sure, needs more brainstorming.

angularsen commented 2 years ago

Hi, I'm not sure if pixels can be considered length units? It depends on the pixels-per-inch of the screen, yes?

If so, I think this is better built outside UnitsNet as helper functions public Length PixelsToLength(double pixels, double pixelsPerInch) { ... }.

ptasev commented 2 years ago

From a conversion to Length perspective, you're right, it depends on several factors. I wanted to point out that in the context of an image, you can measure distance and area in pixels and pixels squared.

Either way, there's no functionality currently in UnitsNet to help users keep track of whether a value is stored as pixels, millipixels, megapixels, micropixels, etc. My team can probably add our own custom UnitsNet type (I still haven't read into it, but I believe you've made it possible), but I figured it would be useful for others as well if it's part of the main library. Just requires some thought and agreement on a final design.

angularsen commented 2 years ago

We solved a similar case here, if you want to take inspiration: https://github.com/angularsen/UnitsNet/blob/c168b8975b7fd5391babf58829762514e37db570/UnitsNet/CustomCode/Wrappers/ReferencePressure.cs

angularsen commented 2 years ago

Closing as will not be fixed.

ptasev commented 2 years ago

What about the use case of storing pixels on their own without having the need to convert to length? Most of our use cases is storing the pixels without doing any conversion (storing millipixels, megapixels, etc). There are very few cases where we take a double pixel value and multiply it by Length pixelSize.

I don't think that's solved by RefrencePressure design. Though I admit I struggled to understand the example in relation to what it might look like for pixels.

The problem is that in some places we store the value as millipixels, and other times as just pixels and it's very easy to make a mistake when summing different values. We have to be very careful about documentation, or variable naming to avoid any mistakes. And even then making a mistake is too likely. That's why I hope to add this to UnitsNet to make it type safe.

Perhaps this can be fixed by adding prefixes to Scalar amount, and using that to represent pixels? "Prefixes": [ "Nano", "Micro", "Milli", "Kilo", "Mega", "Giga" ],

angularsen commented 1 year ago

I understand this can be useful, but I still think this is not widely enough used to be included in the main library and better provided outside. You can still reuse UnitsNet Length when converting pixels to length.

We strive to prevent bloating with too many esoteric quantities and units.