pxconv is a Go package for working with screen units of measurement (dp, sp, px, inch, mm, pt), considering screen density.
- Support for main units:
dp,sp,px,inch,mm,pt. - Customizable screen density: parameters
PxPerDp,PxPerSp, иDpi. - Unit conversion: convenient methods for converting between all supported units.
- Handling of invalid values: default replacement (1 by default) to prevent errors.
To install the package, use the following command:
go get github.com/MiCkEyZzZ/pxconvHere is an example of how to use the package:
package main
import (
"fmt"
"github.com/MiCkEyZzZ/pxconv"
)
func main() {
// Create an instance of Metric with custom density settings
m := pxconv.Metric{PxPerDp: 2, PxPerSp: 1.5, Dpi: 96}
// Convert 10 dp to pixels
px := m.DpToPx(pxconv.Dp(10))
fmt.Println(px) // Output: 20
}The package provides the following key methods for unit conversions:
- DpToPx — Convert dp to px.
- PxToDp — Convert px to dp.
- SpToPx — Convert sp to px.
- PxToSp — Convert px to sp.
- InchToPx — Convert inches to px.
- PxToInch — Convert px to inches.
- MmToPx — Convert millimeters to px.
- PxToMm — Convert px to millimeters.
- PtToPx — Convert points to px.
- PxToPt — Convert px to points.
A full list of methods and their descriptions can be found in the documentation.
This package is distributed under the MIT License. A full copy of the license is available in the License file.