1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Define LogoSetter interface and BestFit selection helper

This commit is contained in:
Achilleas Anagnostopoulos 2017-07-13 07:52:22 +01:00
parent a5c6828fc2
commit cb7ae66556
3 changed files with 78 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package console
import (
"gopheros/device/video/console/font"
"gopheros/device/video/console/logo"
"image/color"
)
@ -68,3 +69,11 @@ type Device interface {
type FontSetter interface {
SetFont(*font.Font)
}
// LogoSetter is an interface implemented by console devices that
// support drawing of logo images.
//
// SetLogo selects the logo to be drawn by the console.
type LogoSetter interface {
SetLogo(*logo.Image)
}

View File

@ -3,9 +3,10 @@ package logo
import "image/color"
// ConsoleLogo defines the logo used by framebuffer consoles. If set to nil
// then no logo will be displayed.
var ConsoleLogo *Image
var (
// The list of available logos.
availableLogos []*Image
)
// Alignment defines the supported horizontal alignments for a console logo.
type Alignment uint8
@ -42,3 +43,29 @@ type Image struct {
// represents an index in the logo palette.
Data []uint8
}
// BestFit returns the best logo from the available logo list given the
// specified console dimensions.
func BestFit(consoleWidth, consoleHeight uint32) *Image {
var (
best *Image
bestDelta, absDelta uint32
threshold = consoleHeight / 10
)
for _, l := range availableLogos {
if l.Height > threshold {
absDelta = l.Height - threshold
} else {
absDelta = threshold - l.Height
}
if best == nil || absDelta < bestDelta {
best = l
bestDelta = absDelta
continue
}
}
return best
}

View File

@ -0,0 +1,39 @@
package logo
import "testing"
func TestBestFit(t *testing.T) {
defer func(origList []*Image) {
availableLogos = origList
}(availableLogos)
availableLogos = []*Image{
&Image{Height: 64},
&Image{Height: 96},
&Image{Height: 128},
}
specs := []struct {
consW, consH uint32
expIndex int
}{
{320, 200, 0},
{800, 600, 0},
{1024, 768, 0},
{1280, 1024, 1},
{3000, 3000, 2},
{2500, 1600, 2},
}
for specIndex, spec := range specs {
got := BestFit(spec.consW, spec.consH)
if got == nil {
t.Errorf("[spec %d] unable to find a logo", specIndex)
continue
}
if got.Height != availableLogos[spec.expIndex].Height {
t.Errorf("[spec %d] expected to get logo with height %d; got %d", specIndex, availableLogos[spec.expIndex].Height, got.Height)
}
}
}