Author Topic: Script: DrawSignature (modif)  (Read 13287 times)

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« on: 2007 December 30 00:09:33 »
Hola,

Por fin está terminado el script.

Las modificaciones introducidas permiten al usuario seleccionar por ComboBox las posiciones:

-TOP_LEFT
-TOP_RIGHT
-TOP_MID
-BOTTOM_LEFT
-BOTTOM_RIGHT
-BOTTOM_MID

Conserva todas las prestaciones del original y esta opción está implementada.

Code: [Select]
// ----------------------------------------------------------------------------
// PixInsight JavaScript Runtime API - PJSR Version 1.0
// Copyright (c) 2003-2008 Pleiades Astrophoto. All Rights Reserved.
// ----------------------------------------------------------------------------
// pjsr/DrawSignature.js - Released 2007/12/12 20:03:05 UTC
// ----------------------------------------------------------------------------
// This file is part of the PJSR. The PJSR is a JavaScript API for development
// of scripts on the PixInsight platform (ECMAScript 262-3 compliant).
// The PJSR is provided in the hope that it will be useful, but it is
// distributed "AS IS" WITHOUT ANY WARRANTY; without even the warranties of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------

#feature-id    Sample Scripts > DrawSignature
#feature-info  A sample script that draws an arbitrary text on a corner of the \
               selected image. This script provides a graphical user interface \
               where the user can select the text, font, size and colors of the \
               text to be drawn, among other parameters.
#feature-icon  DrawSignature.xpm

#include <pjsr/Sizer.jsh>
#include <pjsr/FrameStyle.jsh>
#include <pjsr/TextAlign.jsh>
#include <pjsr/StdButton.jsh>
#include <pjsr/StdIcon.jsh>

/*
// Definición de posición del texto
#define TOP_LEFT 0
#define TOP_RIGHT 1
#define BOTTOM_LEFT 2
#define BOTTOM_RIGHT 3
*/


/**
 * A routine to draw an arbitrary text at the lower-left corner of an image.
 * Rutina modificada para dibujar un texto arbitrario en cualquier esquina seleccionada.
 *
 * The data argument provides operating parameters:
 *
 * data.targetView   Image to draw the text over.
 *
 * data.text         The text to draw.
 *
 * data.fontFace     The font to draw with.
 *
 * data.pointSize    The font size in points.
 *
 * data.bold         Whether the text will be drawn with a bold font.
 *
 * data.italic       Whether the text will be drawn with an italic font.
 *
 * data.stretch      The font stretch factor. A stretch factor of 100 draws
 *                   characters with their normal widths. stretch > 100 draws
 *                   wider (extended) characters, and stretch < 100 draws
 *                   compressed characters.
 *
 * data.textColor    The text color. Encoded as a 32-bit integer: AARRGGBB,
 *                   where AA is the 8-bit alpha (transparency) value, and RR,
 *                   GG, BB are the red, green and blue 8-bit values,
 *                   respectively.
 *
 * data.bgColor      The background color, encoded as explained above.
 *
 * data.margin       The outer margin in pixels.
 *
 * data.softEdges    If true, the text will be drawn with extra soft edges;
 *                   normal edges otherwise.
 */
function DrawSignature( data )
{
   // To execute with diagnostics messages, #define the __DEBUG__ macro; e.g.:
   //    run -d=__DEBUG__ signature.js
#ifdef __DEBUG__
   console.writeln(         "text      : ", data.text );
   console.writeln(         "font      : ", data.fontFace );
   console.writeln(         "posicion  : ", data.localPosi ); //variable de definición de posisción
   console.writeln(         "fontSize  : ", data.fontSize );
   console.writeln(         "stretch   : ", data.stretch );
   console.writeln(         "bold      : ", data.bold );
   console.writeln(         "italic    : ", data.italic );
   console.writeln( format( "textColor : %X", data.textColor ) );
   console.writeln( format( "bgColor   : %X", data.bkgColor ) );
   console.writeln(         "margin    : ", data.margin );
   console.writeln(         "soft      : ", data.softEdges );
#endif
   var image = data.targetView.image;

   // We generate the text on an enlarged working bitmap and draw it
   // scaled down on the target image. This ensures a precise rendition with
   // smooth and well antialiased font strokes.
   const antialiasingScaleFactor = 2.5;

   var pointSize = data.pointSize * antialiasingScaleFactor;
   

   // Create the font
   var font = new Font( data.fontFace );
   /*var alinea = data.textAlign*/
   font.pixelSize = data.fontSize;
   if ( data.bold )
      font.bold = true;
   if ( data.italic )
      font.italic = true;
   font.stretchFactor = data.stretch;

#ifdef __DEBUG__
   console.writeln( "Exact font match : ", font.isExactMatch );
   console.writeln( "Font point size  : ", font.pointSize );
#endif

   // Calculate a reasonable inner margin in pixels
   var innerMargin = Math.round( font.pixelSize/6 );

   // Calculate the sizes of our drawing box
   var width = font.width( data.text ) + 2*innerMargin;
   var height = font.ascent + font.descent + 2*innerMargin;

#ifdef __DEBUG__
   console.writeln( "Drawing box sizes : w=", width, ", h=", height );
#endif

   // Create a bitmap where we'll perform all of our drawing work
   var bmp = new Bitmap( width, height );

   // Fill the bitmap with the background color
   bmp.fill( data.bkgColor );

   // Create a graphics context for the working bitmap
   var G = new Graphics( bmp );

   // Select the required drawing tools: font and pen.
   G.font = font;
   G.pen = new Pen( data.textColor );
   G.transparentBackground = true; // draw text with transparent bkg
   G.textAntialiasing = true;

   // Now draw the signature
   G.drawText( innerMargin, height - font.descent - innerMargin, data.text );

   // Finished drawing
   G.end();

   // If soft text has been requested, we apply a convolution with a mild
   // low-pass filter to soften text edges.
   if ( data.softEdges )
   {
      // Create a RGB image with an alpha channel. The alpha channel is
      // necessary to preserve bitmap transparency.
      var simg = new Image( width, height, 4, 1 );

      // Select all channels, including alpha.
      simg.firstSelectedChannel = 0;
      simg.lastSelectedChannel = 3;

      // Fill the whole image with transparent black
      simg.fill( 0 );

      // Blend the bitmap
      simg.blend( bmp );

      // Apply the low-pass filter (feel free to try out other kernel values)
      simg.convolve( [0.05, 0.15, 0.05,
                      0.15, 1.00, 0.15,
                      0.05, 0.15, 0.05] );

      // Render the resulting image back to our working bitmap
      bmp.assign( simg.render() );
   }

   // Generar variables de posición utilizando switch enlazado a data.localPosi del ComboBox
  switch (data.localPosi) {
      case "TOP_LEFT":
         var position_x = data.margin;
         var position_y = data.margin;
      break;
      case "TOP_RIGHT":
         var position_x = image.width - data.margin - width/antialiasingScaleFactor;
         var position_y = data.margin;
      break;
      case "TOP_MID":
         var position_x = ( image.width / 2 ) - ( (width/antialiasingScaleFactor) / 2 );
         var position_y = data.margin;
      break;
      case "BOTTOM_LEFT":
         var position_x = data.margin;
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
      case "BOTTOM_RIGHT":
         var position_x = image.width - data.margin - width/antialiasingScaleFactor;
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
      case "BOTTOM_MID":
         var position_x= ( image.width / 2 ) - ( (width/antialiasingScaleFactor) / 2 );
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
   }

   // Blend our bitmap at the lower left corner of the image
   image.selectedPoint = new Point( position_x, position_y ); //mi posicionamiento

   /*image.selectedPoint = new Point( data.margin,
                     image.height - data.margin - height/antialiasingScaleFactor );*/
   
   image.blend( bmp.scaled( 1.0/antialiasingScaleFactor ) );
}

/**
 * The DrawSignatureData object defines functional parameters for the
 * DrawSignature routine.
 */
function DrawSignatureData()
{
   // Get access to the active image window
   var window = ImageWindow.activeWindow;

   if ( !window.isNull )
   {
      this.targetView = window.currentView;
      this.text = "PixInsight"; //©José Ferrer
      this.localPosi = "TOP_LEFT";
      this.fontFace = "Helvetica";
      this.fontSize = 256; // px
      this.bold = false;
      this.italic = false;
      this.stretch = 120;
      this.textColor = 0xffff7f00;
      this.bkgColor = 0x80000000;
      this.margin = 8;
      this.softEdges = false;
   }
}

// Global DrawSignature parameters.
var data = new DrawSignatureData;

/**
 * DrawSignatureDialog is a graphical user interface to define
 * DrawSignature parameters.
 */
function DrawSignatureDialog()
{
   this.__base__ = Dialog;
   this.__base__();

   //

   var emWidth = this.font.width( 'M' );
   var labelWidth1 = this.font.width( "Target image:" );

   //

   this.helpLabel = new Label( this );
   this.helpLabel.frameStyle = FrameStyle_Box;
   this.helpLabel.margin = 4;
   this.helpLabel.wordWrapping = true;
   this.helpLabel.useRichText = true;
   this.helpLabel.text = "<b>DrawSignature</b> - This script draws an arbitrary text " +
                         "at the select position of an image. You can enter the text to draw " +
                         "and select the font, along with a number of operating parameters below.<br>" +
                         "To apply the script, click the OK button. To close this dialog without " +
                         "making any changes, click the Cancel button.";

   //

   this.targetImage_Label = new Label( this );
   this.targetImage_Label.text = "Target image:";
   this.targetImage_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.targetImage_Label.minWidth = labelWidth1;

   this.targetImage_ViewList = new ViewList( this );
   this.targetImage_ViewList.getAll();
   this.targetImage_ViewList.currentView = data.targetView;
   this.targetImage_ViewList.toolTip = "Select the image to draw the text over";

   this.targetImage_ViewList.onViewSelected = function( view )
   {
      data.targetView = view;
   };

   this.targetImage_Sizer = new HorizontalSizer;
   this.targetImage_Sizer.spacing = 4;
   this.targetImage_Sizer.add( this.targetImage_Label );
   this.targetImage_Sizer.add( this.targetImage_ViewList, 100 );

   //

   this.text_Label = new Label( this );
   this.text_Label.text = "Text:";
   this.text_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.text_Label.minWidth = labelWidth1;

   this.text_Edit = new Edit( this );
   this.text_Edit.text = data.text;
   this.text_Edit.minWidth = 42*emWidth;
   this.text_Edit.toolTip = "Enter the text to draw";

   this.text_Edit.onEditCompleted = function()
   {
      data.text = this.text;
   };

   this.text_Sizer = new HorizontalSizer;
   this.text_Sizer.spacing = 4;
   this.text_Sizer.add( this.text_Label );
   this.text_Sizer.add( this.text_Edit );

   //

   this.fontFace_Label = new Label( this );
   this.fontFace_Label.text = "Face:";
   this.fontFace_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.fontFace_Label.minWidth = labelWidth1 - 4-1;

   this.fontFace_ComboBox = new ComboBox( this );
   this.fontFace_ComboBox.addItem( "Helvetica" );
   this.fontFace_ComboBox.addItem( "Calibri" );
   this.fontFace_ComboBox.addItem( "Times" );
   this.fontFace_ComboBox.addItem( "Courier" );
   this.fontFace_ComboBox.addItem( "SansSerif" );
   this.fontFace_ComboBox.addItem( "Serif" );
   this.fontFace_ComboBox.addItem( "Monospace" );
   this.fontFace_ComboBox.editEnabled = true;
   this.fontFace_ComboBox.editText = data.fontFace;
   this.fontFace_ComboBox.toolTip = "Type a font face to draw with, or select a standard font family.";

   this.fontFace_ComboBox.onEditTextUpdated = function()
   {
      data.fontFace = this.editText;
   };

   this.fontFace_ComboBox.onItemSelected = function( index )
   {
      data.fontFace = this.itemText( index );
   };

   //
   //Mi parte de programa
   this.localPosi_Label = new Label( this );
   this.localPosi_Label.text = "Posición:";
   this.localPosi_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.localPosi_Label.minWidth = labelWidth1 - 4-1;

   this.localPosi_ComboBox = new ComboBox( this );
   this.localPosi_ComboBox.addItem ( "TOP_LEFT" );
   this.localPosi_ComboBox.addItem ( "TOP_RIGHT" );
   this.localPosi_ComboBox.addItem ( "TOP_MID" );
   this.localPosi_ComboBox.addItem ( "BOTTOM_LEFT" );
   this.localPosi_ComboBox.addItem ( "BOTTOM_RIGHT" );
   this.localPosi_ComboBox.addItem ( "BOTTOM_MID" );
   this.localPosi_ComboBox.editEnabled = true;
   this.localPosi_ComboBox.toolTip = "Indica la posición del texto en la Imagen.";

   this.localPosi_ComboBox.onEditTextUpdated = function()
   {
      data.localPosi = this.editText;
   };

   this.localPosi_ComboBox.onItemSelected = function( index )
   {
      data.localPosi = this.itemText( index );
   };

   this.fontFace_Sizer = new HorizontalSizer;
   this.fontFace_Sizer.spacing = 4;
   this.fontFace_Sizer.add( this.fontFace_Label );
   this.fontFace_Sizer.add( this.fontFace_ComboBox, 100 );
   this.fontFace_Sizer.spacing = 4;
   this.fontFace_Sizer.add( this.localPosi_Label );
   this.fontFace_Sizer.add( this.localPosi_ComboBox, 100 );

   //

   this.fontSize_Label = new Label( this );
   this.fontSize_Label.text = "Size (px):";
   this.fontSize_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.fontSize_Label.minWidth = labelWidth1 - 4-1;

   this.fontSize_SpinBox = new SpinBox( this );
   this.fontSize_SpinBox.minValue = 8;
   this.fontSize_SpinBox.maxValue = 4000;
   this.fontSize_SpinBox.value = data.fontSize;
   this.fontSize_SpinBox.toolTip = "Font size in pixels.";

   this.fontSize_SpinBox.onValueUpdated = function( value )
   {
      data.fontSize = value;
   };

   this.bold_CheckBox = new CheckBox( this );
   this.bold_CheckBox.text = "Bold";
   this.bold_CheckBox.checked = data.bold;
   this.bold_CheckBox.toolTip = "Check to draw with a bold typeface.";

   this.bold_CheckBox.onCheck = function( checked )
   {
      data.bold = checked;
   };

   this.italic_CheckBox = new CheckBox( this );
   this.italic_CheckBox.text = "Italic";
   this.italic_CheckBox.checked = data.italic;
   this.italic_CheckBox.toolTip = "Check to draw with an italics typeface.";

   this.italic_CheckBox.onCheck = function( checked )
   {
      data.italic = checked;
   };

   this.stretch_Label = new Label( this );
   this.stretch_Label.text = "Stretch:";
   this.stretch_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;

   this.stretch_SpinBox = new SpinBox( this );
   this.stretch_SpinBox.minValue = 50;
   this.stretch_SpinBox.maxValue = 200;
   this.stretch_SpinBox.value = data.stretch;
   this.stretch_SpinBox.toolTip = "Font stretch factor:\n" +
                                  "stretch = 100 draws characters with their normal widths.\n" +
                                  "stretch > 100 draws wider (extended) characters.\n" +
                                  "stretch < 100 draws compressed characters.";
   this.stretch_SpinBox.onValueUpdated = function( value )
   {
      data.stretch = value;
   };


   this.fontStyle_Sizer = new HorizontalSizer;
   this.fontStyle_Sizer.spacing = 4;
   this.fontStyle_Sizer.add( this.fontSize_Label );
   this.fontStyle_Sizer.add( this.fontSize_SpinBox );
   this.fontStyle_Sizer.addSpacing( 12 );
   this.fontStyle_Sizer.add( this.bold_CheckBox );
   this.fontStyle_Sizer.add( this.italic_CheckBox );
   this.fontStyle_Sizer.addSpacing( 12 );
   this.fontStyle_Sizer.add( this.stretch_Label );
   this.fontStyle_Sizer.add( this.stretch_SpinBox );
   this.fontStyle_Sizer.addStretch();

   //

   this.textColor_Label = new Label( this );
   this.textColor_Label.text = "Text color:";
   this.textColor_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.textColor_Label.minWidth = labelWidth1 - 4-1;

   this.textColor_Edit = new Edit( this );
   this.textColor_Edit.text = format( "%X", data.textColor );
   this.textColor_Edit.minWidth = 14*emWidth;
   this.textColor_Edit.toolTip = "The text color encoded as a 32-bit hexadecimal integer.\n" +
                                 "(AARRGGBB format: AA=alpha (transparency), RR=red, GG=green, BB=blue)";
   this.textColor_Edit.onEditCompleted = function()
   {
      data.textColor = parseInt( this.text, 16 );
      this.text = format( '%X', data.textColor );
   };

   this.bkgColor_Label = new Label( this );
   this.bkgColor_Label.text = "Background:";
   this.bkgColor_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;

   this.bkgColor_Edit = new Edit( this );
   this.bkgColor_Edit.text = format( "%X", data.bkgColor );
   this.bkgColor_Edit.minWidth = 14*emWidth;
   this.bkgColor_Edit.toolTip = "The background color encoded as a 32-bit hexadecimal integer.\n" +
                                "(AARRGGBB format: AA=alpha (transparency), RR=red, GG=green, BB=blue)";
   this.bkgColor_Edit.onEditCompleted = function()
   {
      data.bkgColor = parseInt( this.text, 16 );
      this.text = format( '%X', data.bkgColor );
   };

   this.textColor_Sizer = new HorizontalSizer;
   this.textColor_Sizer.spacing = 4;
   this.textColor_Sizer.add( this.textColor_Label );
   this.textColor_Sizer.add( this.textColor_Edit );
   this.textColor_Sizer.addStretch();
   this.textColor_Sizer.add( this.bkgColor_Label );
   this.textColor_Sizer.add( this.bkgColor_Edit );

   //

   this.font_Sizer = new VerticalSizer;
   this.font_Sizer.margin = 4;
   this.font_Sizer.spacing = 4;
   this.font_Sizer.add( this.fontFace_Sizer );
   this.font_Sizer.add( this.fontStyle_Sizer );
   this.font_Sizer.add( this.textColor_Sizer );

   this.font_GroupBox = new GroupBox( this );
   this.font_GroupBox.title = "Font";
   this.font_GroupBox.sizer = this.font_Sizer;

   //

   this.margin_Label = new Label( this );
   this.margin_Label.text = "Margin (px):";
   this.margin_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.margin_Label.minWidth = labelWidth1;

   this.margin_SpinBox = new SpinBox( this );
   this.margin_SpinBox.minValue = 0;
   this.margin_SpinBox.maxValue = 250;
   this.margin_SpinBox.value = data.margin;
   this.margin_SpinBox.toolTip = "The margin in pixels between the drawing rectangle and the borders of the image.";

   this.margin_SpinBox.onValueUpdated = function( value )
   {
      data.margin = value;
   };

   this.softEdges_CheckBox = new CheckBox( this );
   this.softEdges_CheckBox.text = "Soft edges";
   this.softEdges_CheckBox.checked = data.softEdges;
   this.softEdges_CheckBox.toolTip = "If checked, the text will be drawn with extra soft edges";

   this.softEdges_CheckBox.onCheck = function( checked )
   {
      data.softEdges = checked;
   };

   this.renderOptions_Sizer = new HorizontalSizer;
   this.renderOptions_Sizer.spacing = 4;
   this.renderOptions_Sizer.add( this.margin_Label );
   this.renderOptions_Sizer.add( this.margin_SpinBox );
   this.renderOptions_Sizer.addSpacing( 12 );
   this.renderOptions_Sizer.add( this.softEdges_CheckBox );
   this.renderOptions_Sizer.addStretch();


   //

   this.ok_Button = new PushButton( this );
   this.ok_Button.text = " OK ";

   this.ok_Button.onClick = function()
   {
      this.dialog.ok();
   };

   this.cancel_Button = new PushButton( this );
   this.cancel_Button.text = " Cancel ";

   this.cancel_Button.onClick = function()
   {
      this.dialog.cancel();
   };

   this.buttons_Sizer = new HorizontalSizer;
   this.buttons_Sizer.spacing = 4;
   this.buttons_Sizer.addStretch();
   this.buttons_Sizer.add( this.ok_Button );
   this.buttons_Sizer.add( this.cancel_Button );

   //

   this.sizer = new VerticalSizer;
   this.sizer.margin = 6;
   this.sizer.spacing = 6;
   this.sizer.add( this.helpLabel );
   this.sizer.addSpacing( 4 );
   this.sizer.add( this.targetImage_Sizer );
   this.sizer.add( this.text_Sizer );
   this.sizer.add( this.font_GroupBox );
   this.sizer.add( this.renderOptions_Sizer );
   this.sizer.add( this.buttons_Sizer );

   this.windowTitle = "DrawSignature Script";
   this.adjustToContents();
   this.setFixedSize();
}

// Our dialog inherits all properties and methods from the core Dialog object.
DrawSignatureDialog.prototype = new Dialog;

/*
 * Script entry point.
 */
function main()
{
#ifndef __DEBUG__
   console.hide();
#endif

   if ( !data.targetView )
   {
      var msg = new MessageBox( "There is no active image window!",
                                "DrawSignature Script", StdIcon_Error, StdButton_Ok );
      msg.execute();
      return;
   }

   var dialog = new DrawSignatureDialog();
   for ( ;; )
   {
      if ( !dialog.execute() )
         break;

      // A view must be selected.
      if ( data.targetView.isNull )
      {
         var msg = new MessageBox( "You must select a view to apply this script.",
                                   "DrawSignature Script", StdIcon_Error, StdButton_Ok );
         msg.execute();
         continue;
      }

      // Tell the core application that we are going to change this view.
      // Without doing this, we'd have just read-only access to the view's image.
      data.targetView.beginProcess();

      // Perform our drawing routine.
      DrawSignature( data );

      // Done with view.
      data.targetView.endProcess();

      // Quit after successful execution.
      break;
   }
}

main();

// ----------------------------------------------------------------------------
// EOF pjsr/DrawSignature.js - Released 2007/12/12 20:03:05 UTC

Espero que sea de utilidad para todos los usuarios.

Saludos,
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #1 on: 2007 December 30 00:11:09 »
Perdón, pero las dos últimas líneas no pertenecen al script.
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #2 on: 2007 December 30 00:38:22 »
Para implementarlo dentro del menú de PixInsight --> Script --> Sample scripts, con diferente nombre al original, he modificado el:

"#feature-id    Sample Scripts > DrawSignature" por "#feature-id    Sample Scripts > DrawSignaturePosition"

Saludos,
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline C. Sonnenstein

  • PixInsight Addict
  • ***
  • Posts: 262
    • http://astrosurf.com/astro35mm
Script: DrawSignature (modif)
« Reply #3 on: 2007 December 30 01:24:54 »
Funciona muy bien, Pep. Ahora DrawSignature es mucho más flexible. Gracias.
Carlos Sonnenstein

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Script: DrawSignature (modif)
« Reply #4 on: 2007 December 31 12:17:06 »
Hola Pep

¡Muy bueno! Bienvenido a la programación en PixInsight/PJSR ;)

ya te escribiré por privado para ver cómo podemos poner el script modificado en el juego de scripts estándar de la aplicación.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #5 on: 2008 January 01 09:36:06 »
Gracias Juan,

Voy a realizar unas modificaciones más para los colores de fuente y fondo, por medio de comboBox, ya que es muy pesado el acordarse de los códigos hexa de los colores cada vez.

Estoy en ello.

Saludos,
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #6 on: 2008 January 03 12:47:11 »
Hola Juan,

Si lees este correo, por favor necesito una ayuda.
Como el lenguaje aún no lo domino, necesito que me digas como pasar una variable numérica a cadena...
Mejor te explico lo que estoy haciendo:

Para los colores de texto y fondo he preparado SpinBox para introducir los porcentajes de cada color que queramos, bueno una vez calculado en base 255. Como dispondré de 4 valores para componer el valor final, en este lenguaje ¿como se realiza?, es decir: R=40; G=35; B=60; TP=20. Estos valores los tendré que pasar a hexadecimal....

Bueno mejor espero a que lo leas y me puedas comentar algo, ya que yo me baso en VB6 y esto se hace en nada, pero aquí desconozco los comandos.

Saludos
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline David Serrano

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 503
--
 David Serrano

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #8 on: 2008 January 06 01:01:19 »
Hola David, Mirando los ejemplos que me has pasado, estoy intentando meterlos en el scrip, pero por el momento no consigo que funcione.

Este es el código que he introducido:
Code: [Select]
  hexadecimal = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")

function convierteHexadecimal(num)
{
    var hexaDec = Math.floor(num/16);
    var hexaUni = num - (num * 16);
    return hexadecimal[hexaDec] + hexadecimal[hexaUni];
   
}

function ColorConvert()
{
   color_hexadecimal = convierteHexadecimal(txtcalcR) ;
}
ColorConvert()


Y aquí tengo el calculo para cada valor:
Code: [Select]
  // Calculations of the colors
   var txtcalcR = Math.round( data.txtR * valbaseColor / 100 );
   var txtcalcG = Math.round( data.txtG * valbaseColor / 100 );
   var txtcalcB = Math.round( data.txtB * valbaseColor / 100 );
   var txtcalcTP = Math.round( data.txtTP * valbaseColor / 100 );
   
   //
   var bkgcalcR = Math.round( data.bkgR * valbaseColor / 100 );
   var bkgcalcG = Math.round( data.bkgG * valbaseColor / 100 );
   var bkgcalcB = Math.round( data.bkgB * valbaseColor / 100 );
   var bkgcalcTP = Math.round( data.bkgTP * valbaseColor / 100 );

Esta parte funciona correctamente, ya que he preparado el texto de salida para visualizar los cálculos y aparecen correctamente. Sin embargo cuando visualizo "color_hexadecimal", me aparece como texto "Fundefined".

¿Puedes ayudarme?

Saludos,
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline Pep

  • PixInsight Addict
  • ***
  • Posts: 124
    • COELLO-PEDRAPIQUER
Script: DrawSignature (modif)
« Reply #9 on: 2008 January 06 02:18:28 »
Hola,

Por fin funciona el script para la adición de texto.

En la primera modificación, realicé la posibilidad de colocarlo en cualquier esquina y centro de la imagen.

En esta segunda modificación, he quitado los textos de definición de color y fondo, y los he sustituido por entradas de % para cada canal, creo que así es más comprensible que tener que conocer de forma hexadecimal el valor de cada color.

Las pruebas que he realizado en mi PC funciona.

Code: [Select]

// ----------------------------------------------------------------------------
// PixInsight JavaScript Runtime API - PJSR Version 1.0
// Copyright (c) 2003-2008 Pleiades Astrophoto. All Rights Reserved.
// ----------------------------------------------------------------------------
// Modified for position of text and color by: José Ferrer
// pjsr/TextoPersonalizado-V2.js - Released 2008/01/06 10:16:05 UTC
// ----------------------------------------------------------------------------
// This file is part of the PJSR. The PJSR is a JavaScript API for development
// of scripts on the PixInsight platform (ECMAScript 262-3 compliant).
// The PJSR is provided in the hope that it will be useful, but it is
// distributed "AS IS" WITHOUT ANY WARRANTY; without even the warranties of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------

#feature-id    Sample Scripts > DrawSignaturePosition
#feature-info  A sample script that draws an arbitrary text on a select position of the \
               selected image. This script provides a graphical user interface \
               where the user can select the text, font, size and colors of the \
               text to be drawn, among other parameters.
#feature-icon  DrawSignature.xpm

#include <pjsr/Sizer.jsh>
#include <pjsr/FrameStyle.jsh>
#include <pjsr/TextAlign.jsh>
#include <pjsr/StdButton.jsh>
#include <pjsr/StdIcon.jsh>

/**
 * A routine to draw an arbitrary text at the lower-left corner of an image.
 * Rutina modificada para dibujar un texto arbitrario en cualquier esquina seleccionada.
 *
 * The data argument provides operating parameters:
 *
 * data.targetView   Image to draw the text over.
 *
 * data.text         The text to draw.
 *
 * data.fontFace     The font to draw with.
 *
 * data.pointSize    The font size in points.
 *
 * data.bold         Whether the text will be drawn with a bold font.
 *
 * data.italic       Whether the text will be drawn with an italic font.
 *
 * data.stretch      The font stretch factor. A stretch factor of 100 draws
 *                   characters with their normal widths. stretch > 100 draws
 *                   wider (extended) characters, and stretch < 100 draws
 *                   compressed characters.
 *
 * data.textColor    The text color. Encoded as a 32-bit integer: AARRGGBB,
 *                   where AA is the 8-bit alpha (transparency) value, and RR,
 *                   GG, BB are the red, green and blue 8-bit values,
 *                   respectively.
 *
 * data.bgColor      The background color, encoded as explained above.
 *
 * data.margin       The outer margin in pixels.
 *
 * data.softEdges    If true, the text will be drawn with extra soft edges;
 *                   normal edges otherwise.
 */
function DrawSignature( data )
{
   // To execute with diagnostics messages, #define the __DEBUG__ macro; e.g.:
   //    run -d=__DEBUG__ signature.js
#ifdef __DEBUG__
   console.writeln(         "text      : ", data.text );
   console.writeln(         "font      : ", data.fontFace );
   console.writeln(         "posicion  : ", data.localPosi ); //variable de definición de posisción
   console.writeln(         "fontSize  : ", data.fontSize );
   console.writeln(         "stretch   : ", data.stretch );
   console.writeln(         "bold      : ", data.bold );
   console.writeln(         "italic    : ", data.italic );
   console.writeln(         "txtR      : ", data.txtR ) );   //Text chanel Red
   console.writeln(         "txtG      : ", data.txtG ) );   //Text chanel Gren
   console.writeln(         "txtB      : ", data.txtB ) );   //Text chanel Blue
   console.writeln(         "txtTP     : ", data.txtTP ) ); //Text chanel Transparency
   console.writeln(         "bkgR      : ", data.bkgR ) );   //Backgound chanel Red
   console.writeln(         "bkgG      : ", data.bkgG ) );   //Backgound chanel Gren
   console.writeln(         "bkgB      : ", data.bkgB ) );   //Backgound chanel Blue
   console.writeln(         "bkgTP     : ", data.bkgTP ) ); //Backgound chanel Transparency
   console.writeln( format( "textColor : %X", data.textColor ) );
   console.writeln( format( "bgColor   : %X", data.bkgColor ) );
   console.writeln(         "margin    : ", data.margin );
   console.writeln(         "soft      : ", data.softEdges );
#endif
   var image = data.targetView.image;

   // We generate the text on an enlarged working bitmap and draw it
   // scaled down on the target image. This ensures a precise rendition with
   // smooth and well antialiased font strokes.
   const antialiasingScaleFactor = 2.5;
   const valbaseColor = 255;

   var pointSize = data.pointSize * antialiasingScaleFactor;

   // Create the font
   var font = new Font( data.fontFace );
   /*var alinea = data.textAlign*/
   font.pixelSize = data.fontSize;
   if ( data.bold )
      font.bold = true;
   if ( data.italic )
      font.italic = true;
   font.stretchFactor = data.stretch;

   // Calculations of the colors.
   // Color for the Tetx
   var txtcalcR = Math.floor( data.txtR * valbaseColor / 100 );
   var txtcalcG = Math.floor( data.txtG * valbaseColor / 100 );
   var txtcalcB = Math.floor( data.txtB * valbaseColor / 100 );
   var txtcalcTP = Math.floor( data.txtTP * valbaseColor / 100 );

   // call to the conversion function
   txtcalcRhex = convierteHexadecimal(txtcalcR);
   txtcalcGhex = convierteHexadecimal(txtcalcG);
   txtcalcBhex = convierteHexadecimal(txtcalcB);
   txtcalcTPhex = convierteHexadecimal(txtcalcTP);

   data.textColor = parseInt(txtcalcTPhex + txtcalcRhex + txtcalcGhex + txtcalcBhex, 16);

   // Color for the background
   var bkgcalcR = Math.floor( data.bkgR * valbaseColor / 100 );
   var bkgcalcG = Math.floor( data.bkgG * valbaseColor / 100 );
   var bkgcalcB = Math.floor( data.bkgB * valbaseColor / 100 );
   var bkgcalcTP = Math.floor( data.bkgTP * valbaseColor / 100 );

   // call to the conversion function
   bkgcalcRhex = convierteHexadecimal(bkgcalcR);
   bkgcalcGhex = convierteHexadecimal(bkgcalcG);
   bkgcalcBhex = convierteHexadecimal(bkgcalcB);
   bkgcalcTPhex = convierteHexadecimal(bkgcalcTP);
   
   data.bkgColor = parseInt(bkgcalcTPhex + bkgcalcRhex + bkgcalcGhex + bkgcalcBhex, 16);
   //

#ifdef __DEBUG__
   console.writeln( "Exact font match : ", font.isExactMatch );
   console.writeln( "Font point size  : ", font.pointSize );
#endif

   // Calculate a reasonable inner margin in pixels
   var innerMargin = Math.round( font.pixelSize/6 );


   // Calculate the sizes of our drawing box
   var width = font.width( data.text ) + 2*innerMargin;
   var height = font.ascent + font.descent + 2*innerMargin;

#ifdef __DEBUG__
   console.writeln( "Drawing box sizes : w=", width, ", h=", height );
#endif

   // Create a bitmap where we'll perform all of our drawing work
   var bmp = new Bitmap( width, height );

   // Fill the bitmap with the background color
   bmp.fill( data.bkgColor );

   // Create a graphics context for the working bitmap
   var G = new Graphics( bmp );

   // Select the required drawing tools: font and pen.
   G.font = font;
   G.pen = new Pen( data.textColor );
   G.transparentBackground = true; // draw text with transparent bkg
   G.textAntialiasing = true;

   // Now draw the signature
   G.drawText( innerMargin, height - font.descent - innerMargin, data.text );

   // Finished drawing
   G.end();

   // If soft text has been requested, we apply a convolution with a mild
   // low-pass filter to soften text edges.
   if ( data.softEdges )
   {
      // Create a RGB image with an alpha channel. The alpha channel is
      // necessary to preserve bitmap transparency.
      var simg = new Image( width, height, 4, 1 );

      // Select all channels, including alpha.
      simg.firstSelectedChannel = 0;
      simg.lastSelectedChannel = 3;

      // Fill the whole image with transparent black
      simg.fill( 0 );

      // Blend the bitmap
      simg.blend( bmp );

      // Apply the low-pass filter (feel free to try out other kernel values)
      simg.convolve( [0.05, 0.15, 0.05,
                      0.15, 1.00, 0.15,
                      0.05, 0.15, 0.05] );

      // Render the resulting image back to our working bitmap
      bmp.assign( simg.render() );
   }

   // To generate position variables using switch connected to data.localPosi of the ComboBox
  switch (data.localPosi) {
      case "TOP_LEFT":
         var position_x = data.margin;
         var position_y = data.margin;
      break;
      case "TOP_RIGHT":
         var position_x = image.width - data.margin - width/antialiasingScaleFactor;
         var position_y = data.margin;
      break;
      case "TOP_MID":
         var position_x = ( image.width / 2 ) - ( (width/antialiasingScaleFactor) / 2 );
         var position_y = data.margin;
      break;
      case "BOTTOM_LEFT":
         var position_x = data.margin;
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
      case "BOTTOM_RIGHT":
         var position_x = image.width - data.margin - width/antialiasingScaleFactor;
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
      case "BOTTOM_MID":
         var position_x= ( image.width / 2 ) - ( (width/antialiasingScaleFactor) / 2 );
         var position_y = image.height - data.margin - height/antialiasingScaleFactor;
      break;
   }

   // Blend our bitmap at the lower left corner of the image
   image.selectedPoint = new Point( position_x, position_y ); //mi posicionamiento

   /*image.selectedPoint = new Point( data.margin,
                     image.height - data.margin - height/antialiasingScaleFactor );*/

   image.blend( bmp.scaled( 1.0/antialiasingScaleFactor ) );
}

   // Conversion decimal to hexadecimal.
   hexadecimal = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");

function convierteHexadecimal(num)
{
    var hexaDec = Math.floor(num/16);
    var hexaUni = num - (hexaDec * 16);
    return hexadecimal[hexaDec] + hexadecimal[hexaUni];
};

/**
 * The DrawSignatureData object defines functional parameters for the
 * DrawSignature routine.
 */
function DrawSignatureData()
{
   // Get access to the active image window
   var window = ImageWindow.activeWindow;

   if ( !window.isNull )
   {
      this.targetView = window.currentView;
      this.text = "©José Ferrer";
      this.localPosi = "BOTTOM_LEFT";
      this.fontFace = "Calibri";
      this.fontSize = 175; // px
      this.bold = true;
      this.italic = false;
      this.stretch = 150;
      this.txtR = 100;
      this.txtG = 100;
      this.txtB = 100;
      this.txtTP = 100;
      this.bkgR = 50;
      this.bkgG = 50;
      this.bkgB = 50;
      this.bkgTP = 35;
      this.textColor = 0xffffffff;
      this.bkgColor = 0x597f7f7f;
      this.margin = 8;
      this.softEdges = false;
   }
}

// Global DrawSignature parameters.
var data = new DrawSignatureData;

/**
 * DrawSignatureDialog is a graphical user interface to define
 * DrawSignature parameters.
 */
function DrawSignatureDialog()
{
   this.__base__ = Dialog;
   this.__base__();

   //

   var emWidth = this.font.width( 'M' );
   var labelWidth1 = this.font.width( "Target image:" );

   //

   this.helpLabel = new Label( this );
   this.helpLabel.frameStyle = FrameStyle_Box;
   this.helpLabel.margin = 4;
   this.helpLabel.wordWrapping = true;
   this.helpLabel.useRichText = true;
   this.helpLabel.text = "<b>DrawSignature</b> - This script draws an arbitrary text " +
                         "at the select position of an image. You can enter the text to draw " +
                         "and select the font, along with a number of operating parameters below.<br>" +
                         "To apply the script, click the OK button. To close this dialog without " +
                         "making any changes, click the Cancel button.";

   //

   this.targetImage_Label = new Label( this );
   this.targetImage_Label.text = "Target image:";
   this.targetImage_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.targetImage_Label.minWidth = labelWidth1;

   this.targetImage_ViewList = new ViewList( this );
   this.targetImage_ViewList.getAll();
   this.targetImage_ViewList.currentView = data.targetView;
   this.targetImage_ViewList.toolTip = "Select the image to draw the text over";

   this.targetImage_ViewList.onViewSelected = function( view )
   {
      data.targetView = view;
   };

   this.targetImage_Sizer = new HorizontalSizer;
   this.targetImage_Sizer.spacing = 4;
   this.targetImage_Sizer.add( this.targetImage_Label );
   this.targetImage_Sizer.add( this.targetImage_ViewList, 100 );

   //

   this.text_Label = new Label( this );
   this.text_Label.text = "Text:";
   this.text_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.text_Label.minWidth = labelWidth1;

   this.text_Edit = new Edit( this );
   this.text_Edit.text = data.text;
   this.text_Edit.minWidth = 42*emWidth;
   this.text_Edit.toolTip = "Enter the text to draw";

   this.text_Edit.onEditCompleted = function()
   {
      data.text = this.text;
   };

   this.text_Sizer = new HorizontalSizer;
   this.text_Sizer.spacing = 4;
   this.text_Sizer.add( this.text_Label );
   this.text_Sizer.add( this.text_Edit );

   //

   this.fontFace_Label = new Label( this );
   this.fontFace_Label.text = "Face:";
   this.fontFace_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.fontFace_Label.minWidth = labelWidth1 - 4-1;

   this.fontFace_ComboBox = new ComboBox( this );
   this.fontFace_ComboBox.addItem( "Calibri" );
   this.fontFace_ComboBox.addItem( "Helvetica" );
   this.fontFace_ComboBox.addItem( "Times" );
   this.fontFace_ComboBox.addItem( "Courier" );
   this.fontFace_ComboBox.addItem( "SansSerif" );
   this.fontFace_ComboBox.addItem( "Serif" );
   this.fontFace_ComboBox.addItem( "Monospace" );
   this.fontFace_ComboBox.editEnabled = true;
   this.fontFace_ComboBox.editText = data.fontFace;
   this.fontFace_ComboBox.toolTip = "Type a font face to draw with, or select a standard font family.";

   this.fontFace_ComboBox.onEditTextUpdated = function()
   {
      data.fontFace = this.editText;
   };

   this.fontFace_ComboBox.onItemSelected = function( index )
   {
      data.fontFace = this.itemText( index );
   };

   //
   // Mi parte de programa
   this.localPosi_Label = new Label( this );
   this.localPosi_Label.text = "Posición:";
   this.localPosi_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.localPosi_Label.minWidth = labelWidth1 - 4-1;

   this.localPosi_ComboBox = new ComboBox( this );
   this.localPosi_ComboBox.addItem ( "BOTTOM_LEFT" );
   this.localPosi_ComboBox.addItem ( "BOTTOM_RIGHT" );
   this.localPosi_ComboBox.addItem ( "BOTTOM_MID" );
   this.localPosi_ComboBox.addItem ( "TOP_LEFT" );
   this.localPosi_ComboBox.addItem ( "TOP_RIGHT" );
   this.localPosi_ComboBox.addItem ( "TOP_MID" );
   this.localPosi_ComboBox.editEnabled = true;
   this.localPosi_ComboBox.toolTip = "Indica la posición del texto en la Imagen.";

   this.localPosi_ComboBox.onEditTextUpdated = function()
   {
      data.localPosi = this.editText;
   };

   this.localPosi_ComboBox.onItemSelected = function( index )
   {
      data.localPosi = this.itemText( index );
   };

   this.fontFace_Sizer = new HorizontalSizer;
   this.fontFace_Sizer.spacing = 4;
   this.fontFace_Sizer.add( this.fontFace_Label );
   this.fontFace_Sizer.add( this.fontFace_ComboBox, 100 );
   this.fontFace_Sizer.spacing = 4;
   this.fontFace_Sizer.add( this.localPosi_Label );
   this.fontFace_Sizer.add( this.localPosi_ComboBox, 100 );

   //

   this.fontSize_Label = new Label( this );
   this.fontSize_Label.text = "Size (px):";
   this.fontSize_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.fontSize_Label.minWidth = labelWidth1 - 4-1;

   this.fontSize_SpinBox = new SpinBox( this );
   this.fontSize_SpinBox.minValue = 8;
   this.fontSize_SpinBox.maxValue = 4000;
   this.fontSize_SpinBox.value = data.fontSize;
   this.fontSize_SpinBox.toolTip = "Font size in pixels.";

   this.fontSize_SpinBox.onValueUpdated = function( value )
   {
      data.fontSize = value;
   };

   this.bold_CheckBox = new CheckBox( this );
   this.bold_CheckBox.text = "Bold";
   this.bold_CheckBox.checked = data.bold;
   this.bold_CheckBox.toolTip = "Check to draw with a bold typeface.";

   this.bold_CheckBox.onCheck = function( checked )
   {
      data.bold = checked;
   };

   this.italic_CheckBox = new CheckBox( this );
   this.italic_CheckBox.text = "Italic";
   this.italic_CheckBox.checked = data.italic;
   this.italic_CheckBox.toolTip = "Check to draw with an italics typeface.";

   this.italic_CheckBox.onCheck = function( checked )
   {
      data.italic = checked;
   };

   this.stretch_Label = new Label( this );
   this.stretch_Label.text = "Stretch:";
   this.stretch_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;

   this.stretch_SpinBox = new SpinBox( this );
   this.stretch_SpinBox.minValue = 50;
   this.stretch_SpinBox.maxValue = 200;
   this.stretch_SpinBox.value = data.stretch;
   this.stretch_SpinBox.toolTip = "Font stretch factor:\n" +
                                  "stretch = 100 draws characters with their normal widths.\n" +
                                  "stretch > 100 draws wider (extended) characters.\n" +
                                  "stretch < 100 draws compressed characters.";
   this.stretch_SpinBox.onValueUpdated = function( value )
   {
      data.stretch = value;
   };


   this.fontStyle_Sizer = new HorizontalSizer;
   this.fontStyle_Sizer.spacing = 4;
   this.fontStyle_Sizer.add( this.fontSize_Label );
   this.fontStyle_Sizer.add( this.fontSize_SpinBox );
   this.fontStyle_Sizer.addSpacing( 12 );
   this.fontStyle_Sizer.add( this.bold_CheckBox );
   this.fontStyle_Sizer.add( this.italic_CheckBox );
   this.fontStyle_Sizer.addSpacing( 12 );
   this.fontStyle_Sizer.add( this.stretch_Label );
   this.fontStyle_Sizer.add( this.stretch_SpinBox );
   this.fontStyle_Sizer.addStretch();

   //
   // Selection of the color for text

   this.textColor_Label = new Label( this );
   this.textColor_Label.text = "Text Color:";
   this.textColor_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;
   this.textColor_Label.minWidth = labelWidth1 - 4-1;

   //Etiquetas %
   this.txtRch_Label = new Label( this );
   this.txtRch_Label.text = "Red %:";
   this.txtRch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.txtGch_Label = new Label( this );
   this.txtGch_Label.text = "Green %:";
   this.txtGch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.txtBch_Label = new Label( this );
   this.txtBch_Label.text = "Blue %:";
   this.txtBch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.txtTPch_Label = new Label( this );
   this.txtTPch_Label.text = "Transparency %:";
   this.txtTPch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   //------------------------------------------------------------------------
   this.txtRch_SpinBox = new SpinBox( this );
   this.txtRch_SpinBox.minValue = 0;
   this.txtRch_SpinBox.maxValue = 100;
   this.txtRch_SpinBox.toolTip = "Range 0% to 100%";
   this.txtRch_SpinBox.value = data.txtR;
   this.txtRch_SpinBox.onValueUpdated = function( value )
   {
      data.txtR = value;
   }

   //------------------------------------------------------------------------
   this.txtGch_SpinBox = new SpinBox( this );
   this.txtGch_SpinBox.minValue = 0;
   this.txtGch_SpinBox.maxValue = 100;
   this.txtGch_SpinBox.toolTip = "Range 0% to 100%";
   this.txtGch_SpinBox.value = data.txtG;
   this.txtGch_SpinBox.onValueUpdated = function( value )
   {
      data.txtG = value;
   }

   //------------------------------------------------------------------------
   this.txtBch_SpinBox = new SpinBox( this );
   this.txtBch_SpinBox.minValue = 0;
   this.txtBch_SpinBox.maxValue = 100;
   this.txtBch_SpinBox.toolTip = "Range 0% to 100%";
   this.txtBch_SpinBox.value = data.txtB;
   this.txtBch_SpinBox.onValueUpdated = function( value )
   {
      data.txtB = value;
   }

   //------------------------------------------------------------------------
   this.txtTPch_SpinBox = new SpinBox( this );
   this.txtTPch_SpinBox.minValue = 0;
   this.txtTPch_SpinBox.maxValue = 100;
   this.txtTPch_SpinBox.toolTip = "Range 0% to 100%";
   this.txtTPch_SpinBox.value = data.txtTP;
   this.txtTPch_SpinBox.onValueUpdated = function( value )
   {
      data.txtR = value;
   }

   //
   // Selection of the color for Background

   this.bkgColor_Label = new Label( this );
   this.bkgColor_Label.text = "Background:";
   this.bkgColor_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;

      // Labels %
   this.bkgRch_Label = new Label( this );
   this.bkgRch_Label.text = "Red %:";
   this.bkgRch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.bkgGch_Label = new Label( this );
   this.bkgGch_Label.text = "Green %:";
   this.bkgGch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.bkgBch_Label = new Label( this );
   this.bkgBch_Label.text = "Blue %:";
   this.bkgBch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   this.bkgTPch_Label = new Label( this );
   this.bkgTPch_Label.text = "Transparency %:";
   this.bkgTPch_Label.textAlignment = TextAlign_Left|TextAlign_VertCenter;

   //------------------------------------------------------------------------
   this.bkgRch_SpinBox = new SpinBox( this );
   this.bkgRch_SpinBox.minValue = 0;
   this.bkgRch_SpinBox.maxValue = 100;
   this.bkgRch_SpinBox.toolTip = "Range 0% to 100%";
   this.bkgRch_SpinBox.value = data.bkgR;
   this.bkgRch_SpinBox.onValueUpdated = function( value )
   {
      data.bkgR = value;
   }

   //------------------------------------------------------------------------
   this.bkgGch_SpinBox = new SpinBox( this );
   this.bkgGch_SpinBox.minValue = 0;
   this.bkgGch_SpinBox.maxValue = 100;
   this.bkgGch_SpinBox.toolTip = "Range 0% to 100%";
   this.bkgGch_SpinBox.value = data.bkgG;
   this.bkgGch_SpinBox.onValueUpdated = function( value )
   {
      data.bkgG = value;
   }

   //------------------------------------------------------------------------
   this.bkgBch_SpinBox = new SpinBox( this );
   this.bkgBch_SpinBox.minValue = 0;
   this.bkgBch_SpinBox.maxValue = 100;
   this.bkgBch_SpinBox.toolTip = "Range 0% to 100%";
   this.bkgBch_SpinBox.value = data.bkgB;
   this.bkgBch_SpinBox.onValueUpdated = function( value )
   {
      data.bkgB = value;
   }

   //------------------------------------------------------------------------
   this.bkgTPch_SpinBox = new SpinBox( this );
   this.bkgTPch_SpinBox.minValue = 0;
   this.bkgTPch_SpinBox.maxValue = 100;
   this.bkgTPch_SpinBox.toolTip = "Range 0% to 100%";
   this.bkgTPch_SpinBox.value = data.bkgTP;
   this.bkgTPch_SpinBox.onValueUpdated = function( value )
   {
      data.bkgTP = value;
   }

   // Old part of the program
/*

   this.textColor_Edit = new Edit( this );
   this.textColor_Edit.text = format( "%X", data.textColor );
   this.textColor_Edit.minWidth = 14*emWidth;
   this.textColor_Edit.toolTip = "The text color encoded as a 32-bit hexadecimal integer.\n" +
                                 "(AARRGGBB format: AA=alpha (transparency), RR=red, GG=green, BB=blue)";
   this.textColor_Edit.onEditCompleted = function()
   {
      data.textColor = parseInt( this.text, 16 );
      this.text = format( '%X', data.textColor );
   };

   this.bkgColor_Label = new Label( this );
   this.bkgColor_Label.text = "Background:";
   this.bkgColor_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;

   this.bkgColor_Edit = new Edit( this );
   this.bkgColor_Edit.text = format( "%X", data.bkgColor );
   this.bkgColor_Edit.minWidth = 14*emWidth;
   this.bkgColor_Edit.toolTip = "The background color encoded as a 32-bit hexadecimal integer.\n" +
                                "(AARRGGBB format: AA=alpha (transparency), RR=red, GG=green, BB=blue)";
   this.bkgColor_Edit.onEditCompleted = function()
   {
      data.bkgColor = parseInt( this.text, 16 );
      this.text = format( '%X', data.bkgColor );
   };

*/

   // Heads
   this.textColor_Sizer = new HorizontalSizer;
   this.textColor_Sizer.spacing = 4;
   this.textColor_Sizer.add( this.textColor_Label );
   this.textColor_Sizer.spacing = 160;
   this.textColor_Sizer.add(this.bkgColor_Label );
   this.textColor_Sizer.addStretch();

   // Selectors and Values of both heads. Line 1
   this.txtSelect1_Sizer = new HorizontalSizer;
   this.txtSelect1_Sizer.spacing = 1;
   this.txtSelect1_Sizer.addSpacing(54);
   this.txtSelect1_Sizer.add( this.txtRch_Label );
   this.txtSelect1_Sizer.add( this.txtRch_SpinBox );
   this.txtSelect1_Sizer.addSpacing(140);
   this.txtSelect1_Sizer.add( this.bkgRch_Label );
   this.txtSelect1_Sizer.add( this.bkgRch_SpinBox );
   this.txtSelect1_Sizer.addStretch();

   // Selectors and Values of both heads. Line 2
   this.txtSelect2_Sizer = new HorizontalSizer;
   this.txtSelect2_Sizer.spacing = 1;
   this.txtSelect2_Sizer.addSpacing(44);
   this.txtSelect2_Sizer.add( this.txtGch_Label );
   this.txtSelect2_Sizer.add( this.txtGch_SpinBox );
   this.txtSelect2_Sizer.addSpacing(130);
   this.txtSelect2_Sizer.add( this.bkgGch_Label );
   this.txtSelect2_Sizer.add( this.bkgGch_SpinBox );
   this.txtSelect2_Sizer.addStretch();

   // Selectors and Values of both heads. Line 3
   this.txtSelect3_Sizer = new HorizontalSizer;
   this.txtSelect3_Sizer.spacing = 1;
   this.txtSelect3_Sizer.addSpacing(53);
   this.txtSelect3_Sizer.add( this.txtBch_Label );
   this.txtSelect3_Sizer.add( this.txtBch_SpinBox );
   this.txtSelect3_Sizer.addSpacing(140);
   this.txtSelect3_Sizer.add( this.bkgBch_Label );
   this.txtSelect3_Sizer.add( this.bkgBch_SpinBox );
   this.txtSelect3_Sizer.addStretch();

   // Selectors and Values of both heads. Line 4
   this.txtSelect4_Sizer = new HorizontalSizer;
   this.txtSelect4_Sizer.spacing = 1;
   this.txtSelect4_Sizer.addSpacing(8);
   this.txtSelect4_Sizer.add( this.txtTPch_Label );
   this.txtSelect4_Sizer.add( this.txtTPch_SpinBox );
   this.txtSelect4_Sizer.addSpacing(95);
   this.txtSelect4_Sizer.add( this.bkgTPch_Label );
   this.txtSelect4_Sizer.add( this.bkgTPch_SpinBox );
   this.txtSelect4_Sizer.addStretch();

   //

   this.font_Sizer = new VerticalSizer;
   this.font_Sizer.margin = 4;
   this.font_Sizer.spacing = 4;
   this.font_Sizer.add( this.fontFace_Sizer );
   this.font_Sizer.add( this.fontStyle_Sizer );
   this.font_Sizer.add( this.textColor_Sizer );
   this.font_Sizer.add( this.txtSelect1_Sizer );
   this.font_Sizer.add( this.txtSelect2_Sizer );
   this.font_Sizer.add( this.txtSelect3_Sizer );
   this.font_Sizer.add( this.txtSelect4_Sizer );

   this.font_GroupBox = new GroupBox( this );
   this.font_GroupBox.title = "Font";
   this.font_GroupBox.sizer = this.font_Sizer;

   //

   this.margin_Label = new Label( this );
   this.margin_Label.text = "Margin (px):";
   this.margin_Label.textAlignment = TextAlign_Right|TextAlign_VertCenter;
   this.margin_Label.minWidth = labelWidth1;

   this.margin_SpinBox = new SpinBox( this );
   this.margin_SpinBox.minValue = 0;
   this.margin_SpinBox.maxValue = 250;
   this.margin_SpinBox.value = data.margin;
   this.margin_SpinBox.toolTip = "The margin in pixels between the drawing rectangle and the borders of the image.";

   this.margin_SpinBox.onValueUpdated = function( value )
   {
      data.margin = value;
   };

   this.softEdges_CheckBox = new CheckBox( this );
   this.softEdges_CheckBox.text = "Soft edges";
   this.softEdges_CheckBox.checked = data.softEdges;
   this.softEdges_CheckBox.toolTip = "If checked, the text will be drawn with extra soft edges";

   this.softEdges_CheckBox.onCheck = function( checked )
   {
      data.softEdges = checked;
   };

   this.renderOptions_Sizer = new HorizontalSizer;
   this.renderOptions_Sizer.spacing = 4;
   this.renderOptions_Sizer.add( this.margin_Label );
   this.renderOptions_Sizer.add( this.margin_SpinBox );
   this.renderOptions_Sizer.addSpacing( 12 );
   this.renderOptions_Sizer.add( this.softEdges_CheckBox );
   this.renderOptions_Sizer.addStretch();

   //

   this.ok_Button = new PushButton( this );
   this.ok_Button.text = " OK ";

   this.ok_Button.onClick = function()
   {
      this.dialog.ok();
   };

   this.cancel_Button = new PushButton( this );
   this.cancel_Button.text = " Cancel ";

   this.cancel_Button.onClick = function()
   {
      this.dialog.cancel();
   };

   this.buttons_Sizer = new HorizontalSizer;
   this.buttons_Sizer.spacing = 4;
   this.buttons_Sizer.addStretch();
   this.buttons_Sizer.add( this.ok_Button );
   this.buttons_Sizer.add( this.cancel_Button );

   //

   this.sizer = new VerticalSizer;
   this.sizer.margin = 6;
   this.sizer.spacing = 6;
   this.sizer.add( this.helpLabel );
   this.sizer.addSpacing( 4 );
   this.sizer.add( this.targetImage_Sizer );
   this.sizer.add( this.text_Sizer );
   this.sizer.add( this.font_GroupBox );
   this.sizer.add( this.renderOptions_Sizer );
   this.sizer.add( this.buttons_Sizer );

   this.windowTitle = "DrawSignature Script";
   this.adjustToContents();
   this.setFixedSize();
}


// Our dialog inherits all properties and methods from the core Dialog object.
DrawSignatureDialog.prototype = new Dialog;

/*
 * Script entry point.
 */
function main()
{
#ifndef __DEBUG__
   console.hide();
#endif

   if ( !data.targetView )
   {
      var msg = new MessageBox( "There is no active image window!",
                                "DrawSignature Script", StdIcon_Error, StdButton_Ok );
      msg.execute();
      return;
   }

   var dialog = new DrawSignatureDialog();
   for ( ;; )
   {
      if ( !dialog.execute() )
         break;

      // A view must be selected.
      if ( data.targetView.isNull )
      {
         var msg = new MessageBox( "You must select a view to apply this script.",
                                   "DrawSignature Script", StdIcon_Error, StdButton_Ok );
         msg.execute();
         continue;
      }

      // Tell the core application that we are going to change this view.
      // Without doing this, we'd have just read-only access to the view's image.
      data.targetView.beginProcess();

      // Perform our drawing routine.
      DrawSignature( data );

      // Done with view.
      data.targetView.endProcess();

      // Quit after successful execution.
      break;
   }
}

main();

// ----------------------------------------------------------------------------
// EOF pjsr/TextoPersonalizado-V2.js - Released 2008/01/06 10:16:05 UTC


Espero que sea de utilidad.

Saludos,
Pep Ferrer
Observatorio:
COELLO-PEDRAPIQUER
Callosa d'en Sarria (Alicante)

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Script: DrawSignature (modif)
« Reply #10 on: 2008 January 06 11:04:12 »
Hola Pep

¡Buen trabajo! :)

Me alegro de que hayas podido solucionar tus problemas con la representación de los colores. El enlace que te pasó David tiene una rutina para conversión a hexadecimal razonablemente buena, que veo que has podido usar.

Es una modificación muy interesante. Si no tienes inconveniente la incluiremos en el juego de scripts de ejemplo que lleva la aplicación, sustituyendo a la versión anterior. Por supuesto, aparecerán créditos indicando tu autoría para las modificaciones que has realizado. Lo prepararé y te lo enviaré para que des el visto bueno.

Otra cosa, en un momento voy a mover este hilo a la sección de desarrollo de software.

Un saludo.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/