moon

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

moon

The Go moon package generates images of the moon in different phases.

The image can either be a simple two color image or can be a shaded image of the moon itself. The image of the moon used is found at https://www.pexels.com/photo/photo-of-full-moon-975012/.

The phase of the moon is represented by the numbers [-1.0..1.0]. The absolute value of the phase is the percent the moon is illuminated. The sign of the phase determines if it is waxing (negative) or waning (positive). A value of 0.0 is a new moon while 1.0 and -1.0 both are a full moon.

A full cycle starting with a new moon uses the values [0.0..-1.0] for the moon waxing until full. The values [1.0..0.0] shows a full moon waning util new.

Example

package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"github.com/pborman/colors/web"
	"github.com/pborman/moon"
)

func makeIcon(path string, size int, phase float64) {
	img := image.NewRGBA(image.Rect(0, 0, size, size))
	// Draw a moon with the visible part white onto img.
	moon.FillMoonIcon(img, web.White, web.Black, phase)
	// Draw the outline of a full moon in black onto img.
	moon.StrokeMoonIcon(img, web.Black, web.Transparent, 1)

	fd, err := os.Create(path)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	png.Encode(fd, img)
	fd.Close()
}

func makePhoto(path string, size int, phase float64) {
	// Get a photo of the moon with the no-visible part being 33% illuminated.
	img := moon.Draw(size, phase, .33)
	fd, err := os.Create(path)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	png.Encode(fd, img)
	fd.Close()
}

func main() {
	makeIcon("moon-new.png", 128, 0)
	makeIcon("moon-waxing-crescent.png", 128, -0.25)
	makeIcon("moon-waning-gibbous.png", 128, 0.75)
	makeIcon("moon-full.png", 128, 1)

	makePhoto("moon-photo-new.png", 128, 0)
	makePhoto("moon-photo-waxing-crescent.png", 128, -0.25)
	makePhoto("moon-photo-waning-gibbous.png", 128, 0.75)
	makePhoto("moon-photo-full.png", 128, 1)
}

Resulting images

Phase Name Icon Photo
0.0 New Moon new new
-0.25 Waxing Moon waxing crescent waxing crescent
±1.0 Full Moon full full
0.75 Gibbous Moon waning gibbous waning gibbous

Documentation

Overview

Package moon produces images of the moon at different phases and provides information about the moon's location.

The phase of the moon is represented by a floating point number between -1.0 and 1.0 where the absolute value is the percentage of the moon that is visible. Both -1. and 1.0 represent a full moon. Negative numbers indicate a waxing moon and positive numbers indicate a waning moon. A full cycle from new moon to new moon is uses the values 0.0 - -1.0 as it waxes and 1.0 - 0.0 as it wanes.

This package has builtin images of the moon that are 64x64, 256x256, 1024x1025, and 1553x1553. The source image used is the smallest image that is at least big as the size requested. E.g., a 56x56 image will be scaled down from the 64x64 image while a 728x728 will be scaled down from the 1024x1024 image.

The source image of the moon is from https://www.pexels.com/photo/photo-of-full-moon-975012/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Draw

func Draw(size int, phase, shadow float64) image.Image

Draw returns an image of moon with the provided phase of the given size. The shadow is now much illumination the non-visible part should have. A shadow of 0 will make it pure black. A shadow of 1 will not shade it at all.

func DrawFromImage

func DrawFromImage(moon image.Image, phase, shadow float64) image.Image

DrawFromImage is like Draw but the caller provides the image of the moon to use. Other than that is is like Draw.

func DrawPhaseMask

func DrawPhaseMask(gc *draw2dimg.GraphicContext, width, height int, phase float64)

DrawPhaseMask draws a path in to gc for the requested phase of the moon that has the supplied width and height. Typically these are the same value. The mask is always drawn in the box {0,0} with the center of the moon at width/2, height/2. The calling function can then call gc.Stroke() or gc.Fill() to draw or fill in the path.

func FillMoonIcon

func FillMoonIcon(r draw.Image, light, shadow color.Color, phase float64) image.Image

FillMoonIcon draws a 2 color image of the moon with the illuminated portion being drawing in light and the shaded portion drawn in shado.

func StrokeMoonIcon

func StrokeMoonIcon(r draw.Image, light, shadow color.Color, phase float64)

StrokMeoonIcon is similar to DrawMoonIcon but only draws the outline. A full circle is always drawn first using the color shadow and the illuminated portions of the moon is drawn with the light color.

Types

type Information added in v0.2.0

type Information struct {
	Moonrise     time.Time       // When the moon will/did rise
	Moonset      time.Time       // When the moon will/did set
	Direction    float64         // Direction to the moon
	Elevation    float64         // Elevation of the moon
	Illumination float64         // How illuminated the moon is
	Phase        sampa.MoonPhase // The current phase
}

Information provides information about the moon. The illumination is a value between -1.0 and 1.0. The amount of illumination is the absolute value (e.g., -0.25 and 0.25 both indicate 25% illumination). If Illumination is negative then the moon is waxing. If positive the moon is waning. An illumination of 0.0 is a new moon and either -1.0 or 1.0 is a full moon.

func Info added in v0.2.0

func Info(when time.Time, loc sampa.Location) (*Information, error)

Info returns information about the moon at a specified location and time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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