protected OMGraphic createImage(Projection projection){
  if (currentTime)   overlayTime=System.currentTimeMillis();
  if (Debug.debugging("daynight")) {
    Debug.output("DayNightLayer: Calculating sun position at time " + Long.toString(overlayTime));
  }
  LatLonPoint brightPoint=SunPosition.sunPosition(overlayTime);
  Debug.message("daynight","DayNightLayer: Calculated sun position");
  if (doPolyTerminator) {
    Debug.message("daynight","DayNightLayer:  Creating polygon terminator");
    LatLonPoint darkPoint=brightPoint.getPoint(Math.PI,Math.PI / 4);
    OMCircle circle=new OMCircle((float)darkPoint.getY(),(float)darkPoint.getX(),(projection instanceof Cylindrical) ? 90f : 89.0f,Length.DECIMAL_DEGREE,terminatorVerts);
    circle.setPolarCorrection(true);
    circle.setFillPaint(nighttimeColor);
    circle.setLinePaint(nighttimeColor);
    circle.generate(projection);
    Debug.message("daynight","DayNightLayer: Done creating polygon terminator");
    return circle;
  }
  int width=projection.getWidth();
  int height=projection.getHeight();
  int[] pixels=new int[width * height];
  OMRaster ret=new OMRaster((int)0,(int)0,width,height,pixels);
  Debug.message("daynight",getName() + "|createImage: Center of bright spot lat= " + brightPoint.getLatitude()+ ", lon= "+ brightPoint.getLongitude());
  int light=daytimeColor.getRGB();
  LatLonPoint testPoint=new LatLonPoint.Float(0f,0f);
  double distance;
  int dark=nighttimeColor.getRGB();
  int darkness=dark >>> 24;
  int value;
  float upperFadeLimit=(float)(MoreMath.HALF_PI * (1.0 + termFade));
  float lowerFadeLimit=(float)(MoreMath.HALF_PI * (1.0 - termFade));
  int fadeColorValue=0x00FFFFFF & (dark);
  for (int i=0; i < width; i++) {
    for (int j=0; j < height; j++) {
      projection.inverse(i,j,testPoint);
      distance=brightPoint.distance(testPoint);
      if (distance > upperFadeLimit) {
        pixels[j * width + i]=dark;
      }
 else       if (distance > lowerFadeLimit) {
        value=(int)(darkness * (1 - ((upperFadeLimit - distance) / (upperFadeLimit - lowerFadeLimit))));
        value<<=24;
        pixels[j * width + i]=fadeColorValue | value;
      }
 else {
        pixels[j * width + i]=light;
      }
    }
  }
  ret.generate(projection);
  return ret;
}

protected OMGraphic createImage ( Projection projection ) { if ( currentTime ) overlayTime = System . currentTimeMillis ( ) ; if ( Debug . debugging ( "daynight" ) ) { Debug . output ( "DayNightLayer: Calculating sun position at time " + Long . toString ( overlayTime ) ) ; } LatLonPoint brightPoint = SunPosition . sunPosition ( overlayTime ) ; Debug . message ( "daynight" , "DayNightLayer: Calculated sun position" ) ; if ( doPolyTerminator ) { Debug . message ( "daynight" , "DayNightLayer:  Creating polygon terminator" ) ; LatLonPoint darkPoint = brightPoint . getPoint ( Math . PI , Math . PI / 4 ) ; OMCircle circle = new OMCircle ( ( float ) darkPoint . getY ( ) , ( float ) darkPoint . getX ( ) , ( projection instanceof Cylindrical ) ? 90f : 89.0f , Length . DECIMAL_DEGREE , terminatorVerts ) ; circle . setPolarCorrection ( true ) ; circle . setFillPaint ( nighttimeColor ) ; circle . setLinePaint ( nighttimeColor ) ; circle . generate ( projection ) ; Debug . message ( "daynight" , "DayNightLayer: Done creating polygon terminator" ) ; return circle ; } int width = projection . getWidth ( ) ; int height = projection . getHeight ( ) ; int [ ] pixels = new int [ width * height ] ; OMRaster ret = new OMRaster ( ( int ) 0 , ( int ) 0 , width , height , pixels ) ; Debug . message ( "daynight" , getName ( ) + "|createImage: Center of bright spot lat= " + brightPoint . getLatitude ( ) + ", lon= " + brightPoint . getLongitude ( ) ) ; int light = daytimeColor . getRGB ( ) ; LatLonPoint testPoint = new LatLonPoint . Float ( 0f , 0f ) ; double distance ; int dark = nighttimeColor . getRGB ( ) ; int darkness = dark > > > 24 ; int value ; float upperFadeLimit = ( float ) ( MoreMath . HALF_PI * ( 1.0 + termFade ) ) ; float lowerFadeLimit = ( float ) ( MoreMath . HALF_PI * ( 1.0 - termFade ) ) ; int fadeColorValue = 0x00FFFFFF & ( dark ) ; for ( int i = 0 ; i < width ; i ++ ) { for ( int j = 0 ; j < height ; j ++ ) { projection . inverse ( i , j , testPoint ) ; distance = brightPoint . distance ( testPoint ) ; if ( distance > upperFadeLimit ) { pixels [ j * width + i ] = dark ; } else if ( distance > lowerFadeLimit ) { value = ( int ) ( darkness * ( 1 - ( ( upperFadeLimit - distance ) / ( upperFadeLimit - lowerFadeLimit ) ) ) ) ; value <<= 24 ; pixels [ j * width + i ] = fadeColorValue | value ; } else { pixels [ j * width + i ] = light ; } } } ret . generate ( projection ) ; return ret ; }
create the omgraphic that acts as an overlay showing the day / night terminator . the brightest spot on the earth is calculated , and then each pixel is inverse projected to find out its coordinates . then the great circle distance is calculated . the terminator is assumed to be the great circle where all the points are pi / 2 away from the bright point . if the termfade variable is set , then the difference in color over the terminator is feathered , on equal amount of the terminator .
