From aa0851bc45d3a999314775df9744311a8dcfbc2c Mon Sep 17 00:00:00 2001 From: Neil Toronto Date: Mon, 16 Jan 2012 16:38:26 -0700 Subject: [PATCH] Exposed simple rendering API (can at least render simple icons given a pict now) Please merge into release --- collects/images/icons/style.rkt | 45 ++++++++++--------- .../images/scribblings/compile-time.scrbl | 2 +- collects/images/scribblings/icons.scrbl | 39 +++++++++++++++- 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/collects/images/icons/style.rkt b/collects/images/icons/style.rkt index 94312b185a..b1f5da12a7 100644 --- a/collects/images/icons/style.rkt +++ b/collects/images/icons/style.rkt @@ -14,10 +14,12 @@ plastic-icon-material glass-icon-material metal-icon-material + bitmap-render-icon (activate-contract-out default-icon-height toolbar-icon-height - default-icon-material) + default-icon-material + icon-color->outline-color) (only-doc-out (all-defined-out))) (defthing light-metal-icon-color (or/c string? (is-a?/c color%)) #:document-value @@ -64,6 +66,23 @@ (defparam default-icon-material deep-flomap-material-value? plastic-icon-material) +(defproc (bitmap-render-icon [bitmap (is-a?/c bitmap%)] + [z-ratio (and rational? (>=/c 0)) 5/8] + [material deep-flomap-material-value? (default-icon-material)] + ) (is-a?/c bitmap%) + (let* ([fm (bitmap->flomap bitmap)] + [dfm (flomap->deep-flomap fm)] + [dfm (deep-flomap-icon-style dfm (* 32 z-ratio))]) + (flomap->bitmap (deep-flomap-render-icon dfm material)))) + +(defproc (icon-color->outline-color [color (or/c string? (is-a?/c color%))]) (is-a?/c color%) + (cond [(string? color) (icon-color->outline-color (send the-color-database find-color color))] + [else + (define r (send color red)) + (define g (send color green)) + (define b (send color blue)) + (make-object color% (quotient r 2) (quotient g 2) (quotient b 2))])) + ;; =================================================================================================== ;; Unpublished so far @@ -76,7 +95,6 @@ draw-short-rendered-icon-flomap define-icon-wrappers (activate-contract-out - icon-color->outline-color set-icon-pen)) (defproc (set-icon-pen [dc (is-a?/c dc<%>)] @@ -85,14 +103,6 @@ [style symbol?]) void? (send dc set-pen (make-object pen% color width style 'projecting 'miter))) -(defproc (icon-color->outline-color [color (or/c string? (is-a?/c color%))]) (is-a?/c color%) - (cond [(string? color) (icon-color->outline-color (send the-color-database find-color color))] - [else - (define r (send color red)) - (define g (send color green)) - (define b (send color blue)) - (make-object color% (quotient r 2) (quotient g 2) (quotient b 2))])) - (define icon-lighting (deep-flomap-lighting-value '(0.0 -1.0 1.0) @@ -106,11 +116,11 @@ [deep-flomap-lighting icon-lighting]) (deep-flomap-render dfm))) -(define (deep-flomap-icon-style dfm) +(define (deep-flomap-icon-style dfm [height 20]) (define s (/ (deep-flomap-height dfm) 32)) (let* ([dfm (deep-flomap-emboss dfm (* s 2) (* s 2))] [dfm (deep-flomap-bulge-round dfm (* s 6))] - [dfm (deep-flomap-raise dfm (* s 20))]) + [dfm (deep-flomap-raise dfm (* s height))]) dfm)) (define (draw-icon-flomap w h draw-proc scale) @@ -131,23 +141,16 @@ [fm (flomap-render-icon fm material)]) fm)) -;; TODO: make one of the following functions unnecessary - (define (flomap-render-thin-icon fm material) (define scale (/ (flomap-height fm) 32)) (define dfm (let* ([dfm (flomap->deep-flomap fm)] - [dfm (deep-flomap-icon-style dfm)] - [dfm (deep-flomap-raise dfm (* -12 scale))]) + [dfm (deep-flomap-icon-style dfm 8)]) dfm)) (deep-flomap-render-icon dfm material)) (define (draw-short-rendered-icon-flomap w h proc scale material) - (let* ([fm (draw-icon-flomap w h proc scale)] - [dfm (flomap->deep-flomap fm)] - [dfm (deep-flomap-icon-style dfm)] - [dfm (deep-flomap-raise dfm (* -12 (/ (flomap-height fm) 32)))]) - (deep-flomap-render-icon dfm material))) + (flomap-render-thin-icon (draw-icon-flomap w h proc scale) material)) ;; =================================================================================================== ;; Syntax for writing icon functions diff --git a/collects/images/scribblings/compile-time.scrbl b/collects/images/scribblings/compile-time.scrbl index aa9f6dfac2..aa9f96f9af 100644 --- a/collects/images/scribblings/compile-time.scrbl +++ b/collects/images/scribblings/compile-time.scrbl @@ -17,7 +17,7 @@ @defmodule[images/compile-time] -Producing computed bitmaps can take time, especially those computed by the functions in the @racketmodname[images/icons] and @racketmodname[images/logos] collections. +Producing computed bitmaps can take time. To reduce the startup time of programs that use computed bitmaps, use the macros exported by @racketmodname[images/compile-time] to @italic{compile} them: to embed the computed bitmaps in fully expanded, compiled modules. @margin-note*{This is a form of constant folding, or equivalently a form of @italic{safe} ``3D'' values.} diff --git a/collects/images/scribblings/icons.scrbl b/collects/images/scribblings/icons.scrbl index 53396ec767..ee9c776dc7 100644 --- a/collects/images/scribblings/icons.scrbl +++ b/collects/images/scribblings/icons.scrbl @@ -158,7 +158,8 @@ This is because, while plastic and glass mostly reflect light directly, metal mo (for/list ([material (list plastic-icon-material glass-icon-material metal-icon-material)]) - (bomb-icon light-metal-icon-color dark-metal-icon-color 32 material))] + (bomb-icon light-metal-icon-color dark-metal-icon-color 32 + material))] } @doc-apply[default-icon-material]{ @@ -166,6 +167,42 @@ The material used for rendering most icons and icon parts. There are exceptions; for example, the @racket[floppy-disk-icon] always renders the sliding cover in metal. } +@doc-apply[bitmap-render-icon]{ +Makes a 3D object out of @racket[bitmap] and renders it as an icon. + +The @racket[z-ratio] argument only makes a difference when @racket[material] is transparent, such as @racket[glass-icon-material]. +It controls what fraction of @racket[bitmap]'s height the icon is raised, which in turn affects the refracted shadow under the icon: +the higher the @racket[z-ratio], the lower the shadow. + +@examples[#:eval icons-eval + (define bitmap + (pict->bitmap (colorize (filled-ellipse 64 64) "tomato"))) + (for/list ([z-ratio (in-range 0 2 1/3)]) + (bitmap-render-icon bitmap z-ratio glass-icon-material))] + +More complex shapes than ``embossed and rounded'' are possible with the full rendering API, which will be made public in a later release. +Still, most of the simple icons (such as those in @racketmodname[images/icons/arrow] and @racketmodname[images/icons/control]) can be rendered using only @racket[bitmap-render-icon]. +} + +@doc-apply[icon-color->outline-color]{ +For a given icon color, returns the proper outline @racket[color%]. + +As an example, here is how to duplicate the @racket[record-icon] using @racketmodname[slideshow/pict]: +@interaction[#:eval icons-eval + (define outline-color (icon-color->outline-color "forestgreen")) + (define brush-pict (colorize (filled-ellipse 62 62) "forestgreen")) + (define pen-pict (linewidth 2 (colorize (ellipse 62 62) outline-color))) + (bitmap-render-icon + (pict->bitmap + (inset (cc-superimpose brush-pict pen-pict) 1)) + 5/8 glass-icon-material) + + (record-icon "forestgreen" 64 glass-icon-material)] + +The outline width is usually @racket[(/ height 32)] (in this case, @racket[2]), but not always. +(For example, @racket[recycle-icon] is an exception, as are parts of @racket[floppy-disk-icon].) +} + @;==================================================================================================== @section[#:tag "arrows"]{Arrow Icons}