### Install q2d Go Graphics Library Source: https://github.com/qbradq/q2d/blob/main/README.md This command installs the q2d Go graphics library using the go get command. Ensure you have Go installed and configured. ```bash go get github.com/qbradq/q2d ``` -------------------------------- ### Go: Text Rendering with q2d and Basic Fonts Source: https://github.com/qbradq/q2d/blob/main/README.md Shows how to render text onto an image in Go using the q2d library and basic font faces. This example includes setting the text position, color, font face, and enabling word wrapping. It requires the 'golang.org/x/image/font/basicfont' package. ```go package main import ( "github.com/qbradq/q2d" "golang.org/x/image/font/basicfont" ) func main() { img := q2d.NewImage(200, 100) white := q2d.Color{255, 255, 255, 255} // Use a basic font face (or load a TTF/OTF) face := basicfont.Face7x13 // Draw text at (10, 20) with wrapping enabled img.Text(q2d.Point{10, 20}, white, face, true, "Hello, World!") } ``` -------------------------------- ### Go: Basic Image Setup and Pixel Manipulation with q2d Source: https://github.com/qbradq/q2d/blob/main/README.md Demonstrates creating a new image in memory using q2d, defining a color, and setting/retrieving individual pixels. This is a fundamental operation for direct pixel manipulation in Go. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { // Create a 100x100 image img := q2d.NewImage(100, 100) // Define a color (Red) red := q2d.Color{255, 0, 0, 255} // Set a pixel at (10, 10) img.Set(q2d.Point{10, 10}, red) // Retrieve a pixel c := img.At(q2d.Point{10, 10}) fmt.Printf("Color at (10, 10): %v\n", c) } ``` -------------------------------- ### Go: Build Complex UI with Panels, Text, and Images Source: https://context7.com/qbradq/q2d/llms.txt Combines various q2d features to construct a hierarchical UI layout, including a title bar, sidebar, and main content area. It demonstrates the use of sub-images for paneling, text rendering with wrapping, borders, and scaled image drawing. This example serves as a blueprint for creating more elaborate user interfaces. ```go package main import ( "image" "image/color" "github.com/qbradq/q2d" ) func main() { // Create main window window := q2d.NewImage(640, 480) // Define color scheme bgGray := q2d.Color{200, 200, 200, 255} darkGray := q2d.Color{100, 100, 100, 255} lightGray := q2d.Color{220, 220, 220, 255} borderColor := q2d.Color{60, 60, 60, 255} textColor := q2d.Color{0, 0, 0, 255} // Fill background window.Fill(bgGray) // Draw title bar (0, 0, 640, 30) window.PushSubImage(q2d.Rectangle{0, 0, 640, 30}) window.Fill(darkGray) window.Border(borderColor) window.Text(q2d.Point{10, 8}, lightGray, q2d.FontNormal, false, "Application Title") window.PopSubImage() // Draw left sidebar (0, 30, 150, 450) window.PushSubImage(q2d.Rectangle{0, 30, 150, 450}) window.Fill(lightGray) window.Border(borderColor) // Draw sidebar items items := []string{"Dashboard", "Settings", "Profile", "Help"} for i, item := range items { y := 10 + i*30 window.Text(q2d.Point{10, y}, textColor, q2d.FontNormal, false, item) } window.PopSubImage() // Draw main content area (150, 30, 490, 450) window.PushSubImage(q2d.Rectangle{150, 30, 490, 450}) window.Fill(bgGray) window.Border(borderColor) // Add padding with clip window.PushClip(q2d.Rectangle{10, 10, 470, 430}) // Draw content header window.Text(q2d.Point{0, 0}, textColor, q2d.FontTall, false, "Welcome!") // Draw wrapped paragraph paragraph := "This is a demonstration of the q2d graphics library. It supports hierarchical rendering with sub-images, text with word wrapping, image scaling with alpha blending, and HSL color manipulation." window.Text(q2d.Point{0, 25}, textColor, q2d.FontNormal, true, paragraph) // Draw a sample image at bottom sampleImg := image.NewRGBA(image.Rect(0, 0, 32, 32)) for y := 0; y < 32; y++ { for x := 0; x < 32; x++ { // Checkerboard pattern if (x/8+y/8)%2 == 0 { sampleImg.Set(x, y, color.RGBA{255, 100, 100, 255}) } else { sampleImg.Set(x, y, color.RGBA{100, 100, 255, 255}) } } } window.DrawImageScaled(sampleImg, q2d.Point{0, 100}, 2) window.PopClip() window.PopSubImage() } ``` -------------------------------- ### Draw Basic Shapes and Fill Regions with Q2D Source: https://context7.com/qbradq/q2d/llms.txt Demonstrates how to create an image, fill it with solid colors, and draw lines and borders using the q2d library. It utilizes functions like NewImage, Fill, HLine, VLine, and Border to manipulate pixel data. ```go package main import "github.com/qbradq/q2d" func main() { img := q2d.NewImage(400, 300) // Define colors white := q2d.Color{255, 255, 255, 255} black := q2d.Color{0, 0, 0, 255} red := q2d.Color{255, 0, 0, 255} blue := q2d.Color{0, 0, 255, 255} // Fill entire image with white img.Fill(white) // Draw horizontal line: y=50, from x=10 to x=390, height=2 img.HLine(50, 10, 390, 2, black) // Draw vertical line: x=200, from y=10 to y=290, width=2 img.VLine(200, 10, 290, 2, black) // Draw border around entire image (1 pixel thick) img.Border(blue) // Draw thick horizontal line img.HLine(100, 50, 350, 5, red) // Draw thick vertical line img.VLine(100, 50, 250, 5, red) } ``` -------------------------------- ### Render Text with Word Wrapping and Fonts using Q2D Source: https://context7.com/qbradq/q2d/llms.txt Demonstrates text rendering capabilities in q2d, including basic text display, word wrapping, formatted text using fmt.Sprintf, and the use of various built-in fonts. The Text function supports positioning, color, font style, and wrapping options. ```go package main import ( "github.com/qbradq/q2d" ) func main() { img := q2d.NewImage(400, 300) white := q2d.Color{255, 255, 255, 255} black := q2d.Color{0, 0, 0, 255} red := q2d.Color{255, 0, 0, 255} // Fill background img.Fill(white) // Draw text without wrapping using built-in font img.Text( q2d.Point{10, 10}, black, q2d.FontNormal, false, // No wrapping "Hello, World!", ) // Draw text with word wrapping longText := "This is a long text that will automatically wrap to fit within the available width of the current clipping region." img.Text( q2d.Point{10, 30}, black, q2d.FontNormal, true, // Enable wrapping longText, ) // Draw formatted text (uses fmt.Sprintf internally) name := "Alice" score := 42 img.Text( q2d.Point{10, 100}, red, q2d.FontTall, false, "Player: %s, Score: %d", name, score, ) // Use different font styles img.Text(q2d.Point{10, 130}, black, q2d.FontFantasy, false, "Fantasy Font") img.Text(q2d.Point{10, 150}, black, q2d.FontSciFi, false, "Sci-Fi Font") img.Text(q2d.Point{10, 170}, black, q2d.FontThin, false, "Thin Font") img.Text(q2d.Point{10, 190}, black, q2d.FontAlternative, false, "Alternative Font") img.Text(q2d.Point{10, 210}, black, q2d.FontTallChunky, false, "Tall Chunky") } ``` -------------------------------- ### Go: Image Creation and Direct Pixel Access Source: https://context7.com/qbradq/q2d/llms.txt Illustrates creating a 2D image in Go using q2d.NewImage and performing direct pixel manipulation. It covers setting individual pixel colors, reading pixel values, and accessing the raw pixel data buffer and stride. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { // Create a 200x150 image img := q2d.NewImage(200, 150) // Define colors red := q2d.Color{255, 0, 0, 255} blue := q2d.Color{0, 0, 255, 255} green := q2d.Color{0, 255, 0, 128} // Semi-transparent // Set individual pixels img.Set(q2d.Point{10, 10}, red) img.Set(q2d.Point{11, 10}, blue) img.Set(q2d.Point{12, 10}, green) // Read pixel values c := img.At(q2d.Point{10, 10}) fmt.Printf("Color at (10, 10): RGBA(%d, %d, %d, %d)\n", c.R(), c.G(), c.B(), c.A()) // Direct access to underlying pixel data // Format: [R, G, B, A, R, G, B, A, ...] pixelData := img.Pix stride := img.Stride // Bytes per row fmt.Printf("Image: %dx%d, Stride: %d bytes, Data size: %d bytes\n", img.Rect.Width(), img.Rect.Height(), stride, len(pixelData)) } ``` -------------------------------- ### Hierarchical Rendering with Sub-Images using Q2D Source: https://context7.com/qbradq/q2d/llms.txt Explains the use of PushSubImage and PopSubImage in q2d for creating local coordinate systems, which is crucial for composing UI elements. This allows for relative positioning and scaling within different parts of the image. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { img := q2d.NewImage(300, 300) red := q2d.Color{255, 0, 0, 255} blue := q2d.Color{0, 0, 255, 255} green := q2d.Color{0, 255, 0, 255} // Draw in root coordinate system img.Set(q2d.Point{0, 0}, red) // Absolute (0, 0) // Create sub-image at (100, 100) with size 100x100 // Origin (0,0) in sub-image maps to absolute (100, 100) img.PushSubImage(q2d.Rectangle{100, 100, 100, 100}) // Draw at local (0, 0) -> absolute (100, 100) img.Set(q2d.Point{0, 0}, blue) // Draw at local (50, 50) -> absolute (150, 150) img.Set(q2d.Point{50, 50}, green) // Border draws around current sub-image bounds img.Border(blue) // Draws at absolute (100,100) to (200,200) // Nested sub-images // Create sub-image at local (10, 10) -> absolute (110, 110) img.PushSubImage(q2d.Rectangle{10, 10, 50, 50}) // Local (0, 0) -> absolute (110, 110) img.Set(q2d.Point{0, 0}, red) // Pop back to parent sub-image img.PopSubImage() // Local (10, 10) -> absolute (110, 110) again img.Set(q2d.Point{10, 10}, green) // Pop back to root img.PopSubImage() // Back to absolute coordinates img.Set(q2d.Point{0, 0}, green) // Overwrites red at (0, 0) fmt.Println("Sub-image rendering complete") } ``` -------------------------------- ### Go: Drawing Shapes (Lines, Fills, Borders) with q2d Source: https://github.com/qbradq/q2d/blob/main/README.md Illustrates drawing basic shapes in Go using the q2d library. It covers filling an image with a color, drawing horizontal and vertical lines, and adding a border. Dependencies include the q2d library. ```go package main import "github.com/qbradq/q2d" func main() { img := q2d.NewImage(200, 200) blue := q2d.Color{0, 0, 255, 255} green := q2d.Color{0, 255, 0, 255} // Fill the entire current clip/image with blue img.Fill(blue) // Draw a horizontal line img.HLine(50, 10, 190, 2, green) // y, x1, x2, height, color // Draw a vertical line img.VLine(50, 10, 190, 2, green) // x, y1, y2, width, color // Draw a border around the current bounds img.Border(q2d.Color{255, 255, 255, 255}) } ``` -------------------------------- ### Go: HSL Color Manipulation with q2d for Theming Source: https://github.com/qbradq/q2d/blob/main/README.md Demonstrates color manipulation in the HSL (Hue, Saturation, Lightness) space using Go and the q2d library. It covers methods for darkening, lightening, and adjusting the hue of a given color, useful for creating color variations and theming. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { red := q2d.Color{255, 0, 0, 255} // Darken by 20% darkRed := red.Darken(0.2) // Lighten by 20% lightRed := red.Lighten(0.2) // Shift Hue by 180 degrees (Complementary color) cyan := red.AdjustHue(180) fmt.Printf("Original: %v, Darker: %v, Cyan: %v\n", red, darkRed, cyan) } ``` -------------------------------- ### Manage Drawing State with Clipping Regions using Q2D Source: https://context7.com/qbradq/q2d/llms.txt Illustrates how to use stack-based clipping regions with PushClip and PopClip in the q2d library. This restricts drawing operations to specified rectangular areas, enabling precise control over rendering. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { img := q2d.NewImage(200, 200) red := q2d.Color{255, 0, 0, 255} blue := q2d.Color{0, 0, 255, 255} // Draw without clipping img.Set(q2d.Point{10, 10}, red) // Will be drawn // Push clipping region: only (50,50) to (150,150) can be drawn img.PushClip(q2d.Rectangle{50, 50, 100, 100}) // Inside clip - will be drawn img.Set(q2d.Point{75, 75}, blue) // Outside clip - will be ignored img.Set(q2d.Point{10, 10}, blue) // Not drawn // Nested clipping - intersects with parent clip img.PushClip(q2d.Rectangle{60, 60, 50, 50}) // Now only (60,60) to (110,110) can be drawn img.Set(q2d.Point{80, 80}, red) // Drawn img.Set(q2d.Point{55, 55}, red) // Not drawn (outside nested clip) // Restore previous clip img.PopClip() // Back to (50,50,100,100) img.Set(q2d.Point{55, 55}, red) // Now drawn // Restore to no clipping img.PopClip() img.Set(q2d.Point{10, 10}, blue) // Drawn again fmt.Println("Clipping demonstration complete") } ``` -------------------------------- ### Go: Color Manipulation RGBA and HSL Source: https://context7.com/qbradq/q2d/llms.txt Demonstrates color manipulation in Go using RGBA and HSL color spaces. It covers creating colors, adjusting brightness, hue, saturation, and converting between RGBA and HSL using the q2d Color type. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { // Create colors red := q2d.Color{255, 0, 0, 255} // Darken color by 30% darkRed := red.Darken(0.3) // Lighten color by 30% lightRed := red.Lighten(0.3) // Shift hue by 180 degrees (complementary color) cyan := red.AdjustHue(180) // Adjust saturation (0.5 = 50% less saturated) desaturated := red.AdjustSaturation(0.5) // Convert to HSL h, s, l := red.ToHSL() // h in [0, 360), s and l in [0, 1] // Create from HSL customColor := q2d.FromHSL(120, 1.0, 0.5, 255) // Pure green fmt.Printf("Original RGBA: %v\n", red) fmt.Printf("HSL: H=%.1f, S=%.2f, L=%.2f\n", h, s, l) fmt.Printf("Darkened: %v, Lightened: %v\n", darkRed, lightRed) fmt.Printf("Complementary: %v\n", cyan) fmt.Printf("Custom HSL color: %v\n", customColor) } ``` -------------------------------- ### Go: Render Text with Clipping and Wrapping Source: https://context7.com/qbradq/q2d/llms.txt Renders text within a specified rectangular area, enabling text wrapping and clipping if the content exceeds the boundaries. This is useful for creating text boxes or labels that fit within defined UI elements. It utilizes `PushSubImage` and `PushClip` for layout management. ```go package main import "github.com/qbradq/q2d" func main() { img := q2d.NewImage(400, 300) white := q2d.Color{255, 255, 255, 255} black := q2d.Color{0, 0, 0, 255} blue := q2d.Color{0, 0, 255, 128} img.Fill(white) // Create a text box with clipping textBoxX, textBoxY := 50, 50 textBoxW, textBoxH := 200, 100 // Draw text box border img.PushSubImage(q2d.Rectangle{textBoxX, textBoxY, textBoxW, textBoxH}) img.Border(blue) // Add padding with nested clip img.PushClip(q2d.Rectangle{5, 5, textBoxW - 10, textBoxH - 10}) // Text will wrap within the clipped region text := "This text will wrap within the defined text box boundaries and will be clipped if it exceeds the available height." img.Text( q2d.Point{0, 0}, black, q2d.FontNormal, true, // Enable wrapping text, ) // Restore state img.PopClip() img.PopSubImage() } ``` -------------------------------- ### Go: Clipping and Sub-Images with q2d for Hierarchical Drawing Source: https://github.com/qbradq/q2d/blob/main/README.md Shows how to use clipping and sub-images in Go with q2d for managing drawing regions hierarchically. This is useful for UI elements where child components should not render outside parent boundaries. It demonstrates Push/Pop operations for state management. ```go package main import "github.com/qbradq/q2d" func main() { img := q2d.NewImage(200, 200) red := q2d.Color{255, 0, 0, 255} // Create a sub-image (local coordinate system) // Origin (0,0) in the sub-image is (50, 50) in the main image img.PushSubImage(q2d.Rectangle{50, 50, 100, 100}) // Restrict drawing to a smaller region within the sub-image img.PushClip(q2d.Rectangle{10, 10, 80, 80}) // This point is relative to the sub-image origin // It will be drawn at absolute (60, 60) img.Set(q2d.Point{10, 10}, red) // This point is outside the clipping region, so it won't be drawn img.Set(q2d.Point{0, 0}, red) // Restore state img.PopClip() img.PopSubImage() } ``` -------------------------------- ### Go: Point Arithmetic and Access Source: https://context7.com/qbradq/q2d/llms.txt Demonstrates Point operations in Go, including vector addition, subtraction, scalar multiplication, division, and coordinate access. It utilizes the q2d Point type for these calculations. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { p1 := q2d.Point{10, 20} p2 := q2d.Point{5, 15} // Vector addition sum := p1.Add(p2) // {15, 35} // Vector subtraction diff := p1.Sub(p2) // {5, 5} // Scalar multiplication scaled := p1.Mul(2) // {20, 40} // Scalar division divided := p1.Div(2) // {5, 10} // Access coordinates x := p1.X() // 10 y := p1.Y() // 20 fmt.Printf("Point: (%d, %d)\n", x, y) fmt.Printf("Sum: %v, Diff: %v\n", sum, diff) fmt.Printf("Scaled: %v, Divided: %v\n", scaled, divided) } ``` -------------------------------- ### Go: Draw Scaled Images with Alpha Blending Source: https://context7.com/qbradq/q2d/llms.txt Demonstrates drawing images at different integer scale factors with proper alpha blending. This function is useful for displaying sprites or UI elements at various sizes while maintaining visual fidelity. It handles transparency correctly, rendering only opaque parts of the source image. Clipping can also be applied. ```go package main import ( "image" "image/color" "github.com/qbradq/q2d" ) func main() { // Create destination image dst := q2d.NewImage(400, 400) white := q2d.Color{255, 255, 255, 255} dst.Fill(white) // Create a simple source image (16x16 sprite) src := image.NewRGBA(image.Rect(0, 0, 16, 16)) // Draw a red square with transparent corners for y := 0; y < 16; y++ { for x := 0; x < 16; x++ { if x < 4 && y < 4 || x >= 12 && y < 4 || x < 4 && y >= 12 || x >= 12 && y >= 12 { // Transparent corners src.Set(x, y, color.RGBA{255, 0, 0, 0}) } else { // Opaque red center src.Set(x, y, color.RGBA{255, 0, 0, 255}) } } } // Draw at original size (scale=1) dst.DrawImageScaled(src, q2d.Point{10, 10}, 1) // Draw scaled 2x dst.DrawImageScaled(src, q2d.Point{50, 10}, 2) // Draw scaled 4x dst.DrawImageScaled(src, q2d.Point{100, 10}, 4) // Draw scaled 8x dst.DrawImageScaled(src, q2d.Point{200, 10}, 8) // Draw with clipping dst.PushClip(q2d.Rectangle{200, 200, 100, 100}) dst.DrawImageScaled(src, q2d.Point{180, 180}, 8) // Partially clipped dst.PopClip() } ``` -------------------------------- ### Go: Rectangle Operations and Containment Source: https://context7.com/qbradq/q2d/llms.txt Shows Rectangle operations in Go, including point containment checks, calculating overlap regions, and translating rectangles. It uses the q2d Rectangle and Point types. ```go package main import ( "fmt" "github.com/qbradq/q2d" ) func main() { r1 := q2d.Rectangle{10, 10, 50, 50} // x=10, y=10, w=50, h=50 r2 := q2d.Rectangle{30, 30, 60, 60} // Check point containment p := q2d.Point{25, 25} inside := r1.Contains(p) // true // Calculate overlap region overlap := r1.Overlap(r2) // {30, 30, 30, 30} // Translate rectangle translated := r1.Add(q2d.Point{5, 5}) // {15, 15, 50, 50} // Access properties x, y := r1.X(), r1.Y() // 10, 10 w, h := r1.Width(), r1.Height() // 50, 50 fmt.Printf("Rectangle at (%d, %d), size %dx%d\n", x, y, w, h) fmt.Printf("Contains point: %v\n", inside) fmt.Printf("Overlap: %v\n", overlap) } ``` -------------------------------- ### Font Generation Script (Perl) Source: https://github.com/qbradq/q2d/blob/main/fonts/unscii.txt The `assemble.pl` script is the root source file for generating Unscii fonts. It reads various text files containing character definitions and font settings, such as font sizes, to construct the final font files. This script is essential for the font generation process. ```perl SET fontsizes 8x8,8x16 INCLUDE punctuation.txt INCLUDE numbers.txt INCLUDE math.txt INCLUDE textsymbols.txt INCLUDE latin.txt INCLUDE greek.txt INCLUDE cyrillic.txt INCLUDE hebrew.txt INCLUDE arabic.txt INCLUDE katakana.txt INCLUDE runes.txt INCLUDE wideascii.txt INCLUDE diacritics.txt INCLUDE PATCH INCLUDE diacrcomb.txt INCLUDE symbols.txt INCLUDE arrows.txt ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.