xcassets

package
v0.0.0-...-eab66a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 18 Imported by: 0

README

xcassets

xcassets package

The xcassets package provides methods for declaring and generating Xcode assets. You can choose to generate one-off assets or your entire .xcassets folder.

Features include:

  • Generate app icon, colors, launch images
  • Support for remote images
  • Functional approach
  • Strongly typed
  • Built-in validation with human-readale errors
  • Output to a string or file

See it in action:

Application Icon

package main

import (
	"fmt"
	"log"

	"github.com/illyabusigin/apptools/xcassets"
)

func main() {
	builder := AppIcon("AppIcon", func(b *AppIconBuilder) {
		b.File("./testdata/Icon.png")
		b.Phone().Configure(func(b *AppIconPhone) {
			b.Settings.File("./testdata/Icon.png") // override individual icon configurations
		})
		b.AppStore()
	})

    if err := builder.Validate(); err != nil {
        log.Fatal("Failed validation", err)
    }

	err := builder.SaveTo("./_test/", true)
}

Launch Images

TBD

Assets

package main

import (
	"fmt"
	"log"

	"github.com/illyabusigin/apptools/xcassets"
)

func main() {
	logo := xcassets.Asset("Logo", func(b *xcassets.AssetBuilder) {
		b.Gamut.Any()
		b.Gamut.SRGBAndDisplayP3()
		b.Source.File("./path/to/file.png")

		b.Color(func(d *xcassets.AssetDefinition) {
			d.Devices.Universal()
			d.ColorSpace.SRGB()
			d.Appearance.Any()

            d.Source.File("./path/to/file.png")
		})
	})

	if err := logo.Validate(); err != nil {
        log.Fatal("Failed validation", err)
    }

	err := logo.SaveTo("./_test/", true)
}

Colors

package main

import (
	"fmt"
	"log"

	"github.com/illyabusigin/apptools/xcassets"
)

func main() {
	splashScreenColor := xcassets.Color("SplashScreenColor", func(b *xcassets.ColorBuilder) {
		b.Gamut.Any()
		b.Gamut.SRGBAndDisplayP3()
		b.Color(func(d *xcassets.ColorDefinition) {
			d.Devices.Universal()
			d.ColorSpace.SRGB()
			d.Appearance.Any()

			d.Hex("#262D44")
			d.Alpha(.4)
		})
	})

	output, err := splashScreenColor.Build()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(output)
}

Documentation

Index

Constants

View Source
const MinDimension = 1024

Variables

This section is empty.

Functions

This section is empty.

Types

type AppIconAppStore

type AppIconAppStore struct {
	Source AssetSource
	// contains filtered or unexported fields
}

AppIconAppStore contains confifuration and customization options for the app store marketing icon.

func (*AppIconAppStore) Build

func (b *AppIconAppStore) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconAppStore) Configure

func (b *AppIconAppStore) Configure(f func(*AppIconAppStore))

Configure allows you to override the default source and configuration.

func (*AppIconAppStore) Validate

func (b *AppIconAppStore) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type AppIconBuilder

type AppIconBuilder struct {
	Name string
	AssetSource
	// contains filtered or unexported fields
}

func AppIcon

func AppIcon(name string, f func(b *AppIconBuilder)) *AppIconBuilder

func (*AppIconBuilder) AppStore

func (b *AppIconBuilder) AppStore() *AppIconBuilder

AppStore enables App Store icons.

func (*AppIconBuilder) Build

func (b *AppIconBuilder) Build() (*AppIconOuput, error)

Build will validate and build the app icon.

func (*AppIconBuilder) CarPlay

func (b *AppIconBuilder) CarPlay() *AppIconCarPlay

CarPlay enables CarPlay icons.

func (*AppIconBuilder) Mac

func (b *AppIconBuilder) Mac() *AppIconMac

Mac enables Mac icons.

func (*AppIconBuilder) Phone

func (b *AppIconBuilder) Phone() *AppIconPhone

Phone enables phone icons.

func (*AppIconBuilder) SaveTo

func (b *AppIconBuilder) SaveTo(path string, overwrite bool) error

SaveTo will save the application icon to the specified path.

func (*AppIconBuilder) Tablet

func (b *AppIconBuilder) Tablet() *AppIconTablet

Tablet enables iPad icons.

func (*AppIconBuilder) Validate

func (b *AppIconBuilder) Validate() error

func (*AppIconBuilder) Watch

func (b *AppIconBuilder) Watch() *AppIconWatch

Watch enables Apple Watch icons.

type AppIconCarPlay

type AppIconCarPlay struct {
	Source AssetSource
	Icon   AssetSource
	// contains filtered or unexported fields
}

AppIconCarPlay contains confifuration and customization options for the CarPlay icon.

func (*AppIconCarPlay) Build

func (b *AppIconCarPlay) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconCarPlay) Configure

func (b *AppIconCarPlay) Configure(f func(*AppIconCarPlay))

Configure allows you to override the default source and configuration.

func (*AppIconCarPlay) Validate

func (b *AppIconCarPlay) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type AppIconImage

type AppIconImage struct {
	Size     string `json:"size"`
	Idiom    string `json:"idiom"`
	Filename string `json:"filename"`
	Scale    string `json:"scale"`
	Role     string `json:"role,omitempty"`
	Subtype  string `json:"subtype,omitempty"`
}

type AppIconImageInput

type AppIconImageInput struct {
	Size     float64
	Idiom    string
	Filename string
	Scale    int
	Role     string
	Subtype  string
	Source   AssetSource
}

func (*AppIconImageInput) Image

func (i *AppIconImageInput) Image() AppIconImage

type AppIconMac

type AppIconMac struct {
	Source  AssetSource
	Size16  AssetSource
	Size32  AssetSource
	Size128 AssetSource
	Size256 AssetSource
	Size512 AssetSource
	// contains filtered or unexported fields
}

AppIconMac contains confifuration and customization options for the Mac icon.

func (*AppIconMac) Build

func (b *AppIconMac) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconMac) Configure

func (b *AppIconMac) Configure(f func(*AppIconMac))

Configure allows you to override the default source and configuration.

func (*AppIconMac) Validate

func (b *AppIconMac) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type AppIconOuput

type AppIconOuput struct {
	Inputs []AppIconImageInput `json:"-"`

	Images []AppIconImage `json:"images"`
	Info   AppIconVersion `json:"info"`
}

func (*AppIconOuput) WriteImages

func (o *AppIconOuput) WriteImages(path string) error

type AppIconPhone

type AppIconPhone struct {
	Source       AssetSource
	Notification AssetSource
	Spotlight    AssetSource
	Settings     AssetSource
	App          AssetSource
	// contains filtered or unexported fields
}

AppIconPhone contains confifuration and customization options for the iPhone icon.

func (*AppIconPhone) Build

func (b *AppIconPhone) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconPhone) Configure

func (b *AppIconPhone) Configure(f func(*AppIconPhone))

Configure allows you to override the default source and configuration.

func (*AppIconPhone) Validate

func (b *AppIconPhone) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type AppIconTablet

type AppIconTablet struct {
	Source       AssetSource
	Notification AssetSource
	Settings     AssetSource
	Spotlight    AssetSource
	App          AssetSource
	Pro          AssetSource
	// contains filtered or unexported fields
}

AppIconTablet contains confifuration and customization options for the iPad icon.

func (*AppIconTablet) Build

func (b *AppIconTablet) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconTablet) Configure

func (b *AppIconTablet) Configure(f func(*AppIconTablet))

Configure allows you to override the default source and configuration.

func (*AppIconTablet) Validate

func (b *AppIconTablet) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type AppIconVersion

type AppIconVersion struct {
	Version int    `json:"version"`
	Author  string `json:"author"`
}

type AppIconWatch

type AppIconWatch struct {
	Source       AssetSource
	Notification AssetSource
	Settings     AssetSource
	HomeScreen   AssetSource
	ShortLook    AssetSource
	// contains filtered or unexported fields
}

AppIconWatch contains confifuration and customization options for the Apple Watch icon.

func (*AppIconWatch) Build

func (b *AppIconWatch) Build(name string, s AssetSource) ([]AppIconImageInput, error)

Build will validate and build the icon using the provided parent asset source. The parent asset source will only be used if no source has been specified for this icon.

func (*AppIconWatch) Configure

func (b *AppIconWatch) Configure(f func(*AppIconWatch))

Configure allows you to override the default source and configuration.

func (*AppIconWatch) Validate

func (b *AppIconWatch) Validate(s AssetSource) error

Validate will validate the icon with the provided parent asset source.

type Appearance

type Appearance struct {
	// contains filtered or unexported fields
}

Appearance allows you to configure your color for different appearance types. See https://developer.apple.com/documentation/uikit/uiimage/providing_images_for_different_appearances

func (*Appearance) Any

func (a *Appearance) Any()

Any specifies that this asset is available for any appearances.

func (*Appearance) Dark

func (a *Appearance) Dark()

Dark specifies that this asset is available for dark appearances.

func (*Appearance) HighContrast

func (a *Appearance) HighContrast()

HighContrast specifies that this asset is availbale for high contrast appearances.

func (*Appearance) Light

func (a *Appearance) Light()

Light specifies that this asset is available for light appearances.

type AssetBuilder

type AssetBuilder struct {
	Gamut      Gamut
	Properties AssetProperties
	// contains filtered or unexported fields
}

AssetBuilder contains methods and properties for manipulating asset properties.

func Asset

func Asset(name string, f func(b *AssetBuilder)) *AssetBuilder

Asset creates a named image set with the specified name, returning an `AssetBuilder` that you can use to customize your asset. See https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/ImageSetType.html#//apple_ref/doc/uid/TP40015170-CH25-SW1 for more information.

func (*AssetBuilder) Asset

func (b *AssetBuilder) Asset(f func(d *AssetDefinition)) *AssetBuilder

Asset specifies the asset definition. Certain properties are set by default and can be overridden, specifically:

d.Appearance.Any()

func (*AssetBuilder) Build

func (b *AssetBuilder) Build() (*AssetOutput, error)

Build will construct the Contents.json of the asset and validate the configuration.

func (*AssetBuilder) SaveTo

func (b *AssetBuilder) SaveTo(path string, overwrite bool) error

SaveTo will save the asset to the specified path.

func (*AssetBuilder) Validate

func (b *AssetBuilder) Validate() error

Validate the asset set configuration.

type AssetCompression

type AssetCompression struct {
	Value *string
}

AssetCompression specifies the compression used for the asset.

func (*AssetCompression) Automatic

func (c *AssetCompression) Automatic()

Automatic specifies the image uses an automatic lossy compression.

func (*AssetCompression) GPUBestQuality

func (c *AssetCompression) GPUBestQuality()

GPUBestQuality specifies the image uses a lossy GPU compression format optimized for quality.

func (*AssetCompression) GPUSmallestSize

func (c *AssetCompression) GPUSmallestSize()

GPUSmallestSize specifies the image uses a lossy GPU compression format optimized for memory size

func (*AssetCompression) Inherited

func (c *AssetCompression) Inherited()

Inherited wil inherient the cmompression type from the parent. If no parent is set the type is set to losses compression.

func (*AssetCompression) Lossless

func (c *AssetCompression) Lossless()

Lossless uses lossless compression. This is the default if no compression is specified.

func (*AssetCompression) Lossy

func (c *AssetCompression) Lossy()

Lossy specifies the image uses basic lossy commpression.

type AssetDefinition

type AssetDefinition struct {
	Appearance Appearance
	Devices    Devices

	Source AssetSource
}

AssetDefinition defines the a specific color and its various properties such as colorspace, devices, color gamut.

func (*AssetDefinition) Validate

func (d *AssetDefinition) Validate() error

Validate will ensure that you have a valid `ColorDefinition`, returning any errors.

type AssetImage

type AssetImage struct {
	Filename string `json:"filename"`
	Idiom    string `json:"idiom"`
	Scale    string `json:"scale"`
	Subtype  string `json:"subtype,omitempty"`

	Appearances  []appearance `json:"appearances,omitempty"`
	DisplayGamut string       `json:"display-gamut,omitempty"`
}

AssetImage is used to construct the JSON in Contents.json `images`.

type AssetOutput

type AssetOutput struct {
	Images     []AssetImage     `json:"images"`
	Info       info             `json:"info"`
	Properties *AssetProperties `json:"properties,omitempty"`
	// contains filtered or unexported fields
}

AssetOutput represents the `Contents.json` file used in an image set.

func (*AssetOutput) WriteImages

func (o *AssetOutput) WriteImages(path string) error

WriteImages will write the file in `Images` to the specified path.

type AssetProperties

type AssetProperties struct {
	RenderAs    AssetRendering   `json:"-"`
	Vector      AssetVectorData  `json:"-"`
	Compression AssetCompression `json:"-"`
	// contains filtered or unexported fields
}

AssetProperties contains all properties for the image set.

func (*AssetProperties) Empty

func (p *AssetProperties) Empty() bool

Empty returns a boolean value indicating whether or not the properties are empty.

func (*AssetProperties) MarshalJSON

func (p *AssetProperties) MarshalJSON() ([]byte, error)

MarshalJSON marshals the asset properties to JSON.

type AssetRendering

type AssetRendering struct {
	Value *string
}

AssetRendering contains data specifiying if the image is a template for use with visual effects such as replacing colors

func (*AssetRendering) Default

func (r *AssetRendering) Default()

Default behavior. If the name of the image ends in "Template", use the image as a template, otherwise render it as the original image

func (*AssetRendering) Original

func (r *AssetRendering) Original()

Original renders the image as the original image.

func (*AssetRendering) Template

func (r *AssetRendering) Template()

Template the image as a template for visual effects such as replacing colors.

type AssetSource

type AssetSource struct {
	// contains filtered or unexported fields
}

AssetSource allows you to define a source for your asset. Asset sources can be remote or local.

func (*AssetSource) Apply

func (s *AssetSource) Apply(from AssetSource)

Apply values from the provided asset source to the destination asset source.

func (AssetSource) Empty

func (s AssetSource) Empty() bool

Empty returns a boolean value indicating whether or not the asset source is empty.

func (*AssetSource) File

func (s *AssetSource) File(path string)

File specifies a file path for your asset. This file path will be validated during the validation phase.

func (*AssetSource) MinDimension

func (s *AssetSource) MinDimension(d int)

MinDimension specifies the minimum dimension for the icon image.

func (*AssetSource) Size

func (s *AssetSource) Size(width, height uint)

Size of the asset at 1x scale factor. These dimensions are used to ensure that the source image is the correct size and aspect ratio.

func (*AssetSource) URL

func (s *AssetSource) URL(url string)

URL specifies a URL for the asset. This URL will be verified during the validation phase.

func (*AssetSource) Validate

func (s *AssetSource) Validate() error

Validate will validate the asset source. This validation can include network requests if you specified remote assets in your definition.

type AssetVectorData

type AssetVectorData struct {
	// contains filtered or unexported fields
}

AssetVectorData contains vector data information

func (*AssetVectorData) PreserveVectorData

func (r *AssetVectorData) PreserveVectorData(v bool)

PreserveVectorData set to `true` will preserve the vector information for a PDF file.

type ColorBuilder

type ColorBuilder struct {
	Gamut Gamut
	// contains filtered or unexported fields
}

ColorBuilder contains methods and properties for manipulating color properties.

func Color

func Color(name string, f func(b *ColorBuilder)) *ColorBuilder

Color creates a named color type with the specified name, returning a `ColorBuilder` function that you can use to customize your color. See https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/Named_Color.html#//apple_ref/doc/uid/TP40015170-CH59-SW1 for more information.

func (*ColorBuilder) Build

func (b *ColorBuilder) Build() (string, error)

Build will construct the Contents.json of the color.

func (*ColorBuilder) Color

func (b *ColorBuilder) Color(f func(d *ColorDefinition)) *ColorBuilder

Color specifies the color definition. Certain properties are set by default and can be overridden, specifically:

 d.Appearance.Any()
	d.ColorSpace.SRGB()
 d.Alpha(1)

func (*ColorBuilder) Validate

func (b *ColorBuilder) Validate() error

Validate the color set configuration.

func (*ColorBuilder) Write

func (b *ColorBuilder) Write(w io.Writer) error

Write will write the Contents.json to the specified `io.Writer`.

type ColorDefinition

type ColorDefinition struct {
	// Use the functions on ColorSpace to define the colorspace.
	ColorSpace ColorSpace
	Appearance Appearance
	Devices    Devices
	// contains filtered or unexported fields
}

ColorDefinition defines the a specific color and its various properties such as colorspace, devices, color gamut.

func (*ColorDefinition) Alpha

func (d *ColorDefinition) Alpha(a float64) error

Alpha defines the alpha, or opacity of the color.

func (*ColorDefinition) Hex

func (d *ColorDefinition) Hex(v string) error

Hex specifies a color in hexadecimal format (i.e. #262d44).

func (*ColorDefinition) RGB

func (d *ColorDefinition) RGB(r, g, b int) error

RGB allows you to specify an 8-bit RGB color. Each value must be between 0 and 255.

func (*ColorDefinition) RGBFloat

func (d *ColorDefinition) RGBFloat(r, g, b float64) error

RGBFloat specifies a floating poiny RGB color. Each value must be between 0 and 1.

func (*ColorDefinition) Validate

func (d *ColorDefinition) Validate() error

Validate will ensure that you have a valid `ColorDefinition`, returning any errors.

func (*ColorDefinition) White

func (d *ColorDefinition) White(v float64) error

White defines the white level of your color. Should only be used in conjunction with gray-scale color spaces. Value must be between 0 and 1.

type ColorSpace

type ColorSpace struct {
	// contains filtered or unexported fields
}

ColorSpace allows you to specify the color space for the xcasset. If no colorspace is specifies the sRGB color space is used.

func (*ColorSpace) DisplayP3

func (c *ColorSpace) DisplayP3()

DisplayP3 specifies the asset uses a wide gamut color space.

func (*ColorSpace) ExtendedRangeGray

func (c *ColorSpace) ExtendedRangeGray()

ExtendedRangeGray specifies the asset uses the extended range gray color space.

func (*ColorSpace) ExtendedRangeLinearSRGB

func (c *ColorSpace) ExtendedRangeLinearSRGB()

ExtendedRangeLinearSRGB specifies the asset uses the extended range linear sRGB color space.

func (*ColorSpace) ExtendedRangeSRGB

func (c *ColorSpace) ExtendedRangeSRGB()

ExtendedRangeSRGB specifies the asset uses the extended range sRGB color space.

func (*ColorSpace) GrayGamma22

func (c *ColorSpace) GrayGamma22()

GrayGamma22 specifies the asset uses the gray gamma 2.2 color space.

func (*ColorSpace) SRGB

func (c *ColorSpace) SRGB()

SRGB specifies the asset uses the standard sRGB color space.

type Devices

type Devices struct {
	// contains filtered or unexported fields
}

Devices contains functions for specifying the idioms for an xcasset. See https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/ImageSetType.html#//apple_ref/doc/uid/TP40015170-CH25-SW2 for more information.

func (*Devices) AppleTV

func (d *Devices) AppleTV() *Devices

AppleTV specifies that this asset is for Apple TV.

func (*Devices) AppleWatch

func (d *Devices) AppleWatch() *Devices

AppleWatch specifies that this asset is for Apple Watch devices.

func (*Devices) CarPlay

func (d *Devices) CarPlay() *Devices

CarPlay specifies that this asset is for CarPlay devices.

func (*Devices) Catalyst

func (d *Devices) Catalyst() *Devices

Catalyst specifies that this asset is for iPad and and Mac devices using Catalyst.

func (*Devices) IPad

func (d *Devices) IPad() *Devices

IPad specifies that this asset is for iPhone devices.

func (*Devices) IPhone

func (d *Devices) IPhone() *Devices

IPhone specifies that this asset is for iPhone devices.

func (*Devices) Mac

func (d *Devices) Mac() *Devices

Mac specifies that this asset is for Mac computers.

func (*Devices) Universal

func (d *Devices) Universal() *Devices

Universal specifies that this asset works on any device and platform.

func (*Devices) Validate

func (d *Devices) Validate() error

Validate will validate the devices configuration. At least one specified device is required (i.e. Universal()).

type Gamut

type Gamut struct {
	// contains filtered or unexported fields
}

Gamut specifes the color gamut of the device display. See https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/ImageSetType.html#//apple_ref/doc/uid/TP40015170-CH25-SW35 for more information.

func (*Gamut) Any

func (g *Gamut) Any()

Any specifes the xcasset uses the standard RGB gamut color space.

func (*Gamut) SRGBAndDisplayP3

func (g *Gamut) SRGBAndDisplayP3()

SRGBAndDisplayP3 specifes the xcasses uses the standard RGB and wide gamut color space.

type IconImageBuilder

type IconImageBuilder struct {
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL