.packageName <- "hexbin"
BTC <- function(n, beg = 1, end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(0,0,0),
	       c(0,0,40),
	       c(0,4,56),
	       c(0,9,61),
	       c(0,12,64),
	       c(0,14,66),
	       c(0,17,69),
	       c(0,20,73),
	       c(0,22,74),
	       c(0,25,78),
	       c(0,27,79),
	       c(0,30,83),
	       c(0,31,85),
	       c(0,33,86),
	       c(0,36,90),
	       c(0,38,91),
	       c(0,39,93),
	       c(0,41,95),
	       c(0,43,96),
	       c(0,46,100),
	       c(0,47,102),
	       c(0,49,103),
	       c(0,51,105),
	       c(0,52,107),
	       c(0,54,108),
	       c(0,55,110),
	       c(0,57,112),
	       c(0,57,112),
	       c(0,58,113),
	       c(0,60,115),
	       c(0,62,117),
	       c(0,63,119),
	       c(0,65,120),
	       c(0,66,122),
	       c(0,68,124),
	       c(0,70,125),
	       c(0,71,127),
	       c(0,73,129),
	       c(0,73,129),
	       c(0,74,130),
	       c(0,76,132),
	       c(0,78,134),
	       c(0,79,136),
	       c(0,81,137),
	       c(0,82,139),
	       c(0,84,141),
	       c(0,86,142),
	       c(0,87,144),
	       c(0,89,146),
	       c(0,90,147),
	       c(0,92,149),
	       c(0,94,151),
	       c(0,94,151),
	       c(0,95,153),
	       c(0,97,154),
	       c(0,98,156),
	       c(0,100,158),
	       c(0,102,159),
	       c(0,103,161),
	       c(0,105,163),
	       c(0,106,164),
	       c(0,108,166),
	       c(0,109,168),
	       c(0,111,170),
	       c(0,113,171),
	       c(0,114,173),
	       c(0,116,175),
	       c(0,117,176),
	       c(0,119,178),
	       c(0,121,180),
	       c(0,121,180),
	       c(0,122,181),
	       c(0,124,183),
	       c(0,125,185),
	       c(0,127,187),
	       c(0,129,188),
	       c(0,130,190),
	       c(0,132,192),
	       c(0,133,193),
	       c(0,135,195),
	       c(0,137,197),
	       c(0,138,198),
	       c(0,140,200),
	       c(0,141,202),
	       c(0,143,204),
	       c(0,143,204),
	       c(0,145,205),
	       c(0,146,207),
	       c(0,148,209),
	       c(0,149,210),
	       c(0,151,212),
	       c(0,153,214),
	       c(0,154,215),
	       c(0,156,217),
	       c(0,157,219),
	       c(0,159,221),
	       c(0,160,222),
	       c(0,160,222),
	       c(0,162,224),
	       c(0,164,226),
	       c(0,165,227),
	       c(0,167,229),
	       c(0,168,231),
	       c(0,170,232),
	       c(0,172,234),
	       c(0,173,236),
	       c(0,175,238),
	       c(0,175,238),
	       c(0,176,239),
	       c(0,178,241),
	       c(0,180,243),
	       c(0,181,244),
	       c(0,183,246),
	       c(2,184,248),
	       c(4,186,249),
	       c(4,186,249),
	       c(4,186,249),
	       c(6,188,251),
	       c(6,188,251),
	       c(9,189,253),
	       c(9,189,253),
	       c( 11,191,255),
	       c( 11,191,255),
	       c( 13,192,255),
	       c( 13,192,255),
	       c( 13,192,255),
	       c( 16,194,255),
	       c( 18,196,255),
	       c( 20,197,255),
	       c( 20,197,255),
	       c( 23,199,255),
	       c( 25,200,255),
	       c( 27,202,255),
	       c( 30,204,255),
	       c( 32,205,255),
	       c( 34,207,255),
	       c( 37,208,255),
	       c( 37,208,255),
	       c( 39,210,255),
	       c( 41,211,255),
	       c( 44,213,255),
	       c( 46,215,255),
	       c( 48,216,255),
	       c( 51,218,255),
	       c( 53,219,255),
	       c( 53,219,255),
	       c( 55,221,255),
	       c( 57,223,255),
	       c( 60,224,255),
	       c( 62,226,255),
	       c( 64,227,255),
	       c( 67,229,255),
	       c( 67,229,255),
	       c( 69,231,255),
	       c( 71,232,255),
	       c( 74,234,255),
	       c( 76,235,255),
	       c( 78,237,255),
	       c( 81,239,255),
	       c( 81,239,255),
	       c( 83,240,255),
	       c( 85,242,255),
	       c( 88,243,255),
	       c( 90,245,255),
	       c( 92,247,255),
	       c( 95,248,255),
	       c( 95,248,255),
	       c( 97,250,255),
	       c( 99,251,255),
	       c(102,253,255),
	       c(104,255,255),
	       c(106,255,255),
	       c(106,255,255),
	       c(108,255,255),
	       c(111,255,255),
	       c(113,255,255),
	       c(115,255,255),
	       c(115,255,255),
	       c(118,255,255),
	       c(120,255,255),
	       c(122,255,255),
	       c(122,255,255),
	       c(125,255,255),
	       c(127,255,255),
	       c(129,255,255),
	       c(129,255,255),
	       c(132,255,255),
	       c(134,255,255),
	       c(136,255,255),
	       c(136,255,255),
	       c(139,255,255),
	       c(141,255,255),
	       c(143,255,255),
	       c(143,255,255),
	       c(146,255,255),
	       c(148,255,255),
	       c(150,255,255),
	       c(150,255,255),
	       c(153,255,255),
	       c(155,255,255),
	       c(155,255,255),
	       c(157,255,255),
	       c(159,255,255),
	       c(159,255,255),
	       c(162,255,255),
	       c(164,255,255),
	       c(164,255,255),
	       c(166,255,255),
	       c(169,255,255),
	       c(171,255,255),
	       c(171,255,255),
	       c(173,255,255),
	       c(176,255,255),
	       c(176,255,255),
	       c(178,255,255),
	       c(180,255,255),
	       c(180,255,255),
	       c(183,255,255),
	       c(185,255,255),
	       c(185,255,255),
	       c(187,255,255),
	       c(190,255,255),
	       c(190,255,255),
	       c(192,255,255),
	       c(194,255,255),
	       c(197,255,255),
	       c(197,255,255),
	       c(199,255,255),
	       c(201,255,255),
	       c(204,255,255),
	       c(204,255,255),
	       c(206,255,255),
	       c(208,255,255),
	       c(210,255,255),
	       c(210,255,255),
	       c(213,255,255),
	       c(215,255,255),
	       c(217,255,255),
	       c(217,255,255),
	       c(220,255,255),
	       c(222,255,255),
	       c(224,255,255),
	       c(227,255,255),
	       c(229,255,255),
	       c(229,255,255),
	       c(231,255,255),
	       c(234,255,255),
	       c(236,255,255),
	       c(238,255,255),
	       c(241,255,255),
	       c(243,255,255),
	       c(243,255,255),
	       c(245,255,255),
	       c(248,255,255),
	       c(250,255,255),
	       c(255,255,255))[ round(seq(beg,end, length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)
}


BTY <- function(n, beg = 1, end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(7,7,254),
	       c(23,23,252),
	       c(30,30,250),
	       c(36,36,248),
	       c(40,40,247),
	       c(44,44,245),
	       c(47,47,243),
	       c(50,50,242),
	       c(52,52,240),
	       c(55,55,239),
	       c(57,57,238),
	       c(59,59,236),
	       c(61,61,235),
	       c(63,63,234),
	       c(65,65,233),
	       c(66,66,231),
	       c(68,68,230),
	       c(69,69,229),
	       c(71,71,228),
	       c(72,72,227),
	       c(74,74,226),
	       c(75,75,225),
	       c(76,76,225),
	       c(78,78,224),
	       c(79,79,223),
	       c(80,80,222),
	       c(81,81,221),
	       c(82,82,221),
	       c(84,84,220),
	       c(85,85,219),
	       c(86,86,218),
	       c(87,87,218),
	       c(88,88,217),
	       c(89,89,216),
	       c(90,90,216),
	       c(91,91,215),
	       c(92,92,214),
	       c(93,93,214),
	       c(94,94,213),
	       c(95,95,213),
	       c(96,96,212),
	       c(97,97,212),
	       c(98,98,211),
	       c(98,98,210),
	       c(99,99,210),
	       c(100,100,209),
	       c(101,101,209),
	       c(102,102,208),
	       c(103,103,208),
	       c(104,104,208),
	       c(105,105,207),
	       c(105,105,207),
	       c(106,106,206),
	       c(107,107,206),
	       c(108,108,205),
	       c(109,109,205),
	       c(110,110,204),
	       c(110,110,204),
	       c(111,111,204),
	       c(112,112,203),
	       c(113,113,203),
	       c(114,114,202),
	       c(114,114,202),
	       c(115,115,202),
	       c(116,116,201),
	       c(117,117,201),
	       c(118,118,200),
	       c(118,118,200),
	       c(119,119,200),
	       c(120,120,199),
	       c(121,121,199),
	       c(121,121,199),
	       c(122,122,198),
	       c(123,123,198),
	       c(124,124,198),
	       c(124,124,197),
	       c(125,125,197),
	       c(126,126,197),
	       c(127,127,196),
	       c(128,128,196),
	       c(128,128,195),
	       c(129,129,195),
	       c(130,130,195),
	       c(130,130,194),
	       c(131,131,194),
	       c(132,132,194),
	       c(133,133,193),
	       c(133,133,193),
	       c(134,134,193),
	       c(135,135,192),
	       c(136,136,192),
	       c(136,136,192),
	       c(137,137,191),
	       c(138,138,191),
	       c(139,139,191),
	       c(139,139,190),
	       c(140,140,190),
	       c(141,141,190),
	       c(142,142,189),
	       c(142,142,189),
	       c(143,143,189),
	       c(144,144,188),
	       c(144,144,188),
	       c(145,145,188),
	       c(146,146,187),
	       c(147,147,187),
	       c(147,147,187),
	       c(148,148,186),
	       c(149,149,186),
	       c(149,149,186),
	       c(150,150,185),
	       c(151,151,185),
	       c(152,152,185),
	       c(152,152,184),
	       c(153,153,184),
	       c(154,154,184),
	       c(154,154,183),
	       c(155,155,183),
	       c(156,156,182),
	       c(157,157,182),
	       c(157,157,182),
	       c(158,158,181),
	       c(159,159,181),
	       c(159,159,181),
	       c(160,160,180),
	       c(161,161,180),
	       c(162,162,180),
	       c(162,162,179),
	       c(163,163,179),
	       c(164,164,178),
	       c(164,164,178),
	       c(165,165,178),
	       c(166,166,177),
	       c(167,167,177),
	       c(167,167,176),
	       c(168,168,176),
	       c(169,169,176),
	       c(169,169,175),
	       c(170,170,175),
	       c(171,171,174),
	       c(172,172,174),
	       c(172,172,173),
	       c(173,173,173),
	       c(174,174,173),
	       c(174,174,172),
	       c(175,175,172),
	       c(176,176,171),
	       c(177,177,171),
	       c(177,177,170),
	       c(178,178,170),
	       c(179,179,169),
	       c(179,179,169),
	       c(180,180,168),
	       c(181,181,168),
	       c(181,181,167),
	       c(182,182,167),
	       c(183,183,166),
	       c(184,184,166),
	       c(184,184,165),
	       c(185,185,165),
	       c(186,186,164),
	       c(186,186,164),
	       c(187,187,163),
	       c(188,188,163),
	       c(189,189,162),
	       c(189,189,162),
	       c(190,190,161),
	       c(191,191,161),
	       c(191,191,160),
	       c(192,192,159),
	       c(193,193,159),
	       c(194,194,158),
	       c(194,194,158),
	       c(195,195,157),
	       c(196,196,157),
	       c(196,196,156),
	       c(197,197,155),
	       c(198,198,155),
	       c(199,199,154),
	       c(199,199,153),
	       c(200,200,153),
	       c(201,201,152),
	       c(201,201,151),
	       c(202,202,151),
	       c(203,203,150),
	       c(204,204,149),
	       c(204,204,149),
	       c(205,205,148),
	       c(206,206,147),
	       c(206,206,146),
	       c(207,207,146),
	       c(208,208,145),
	       c(209,209,144),
	       c(209,209,143),
	       c(210,210,143),
	       c(211,211,142),
	       c(211,211,141),
	       c(212,212,140),
	       c(213,213,139),
	       c(214,214,138),
	       c(214,214,138),
	       c(215,215,137),
	       c(216,216,136),
	       c(216,216,135),
	       c(217,217,134),
	       c(218,218,133),
	       c(219,219,132),
	       c(219,219,131),
	       c(220,220,130),
	       c(221,221,129),
	       c(221,221,128),
	       c(222,222,127),
	       c(223,223,126),
	       c(224,224,125),
	       c(224,224,124),
	       c(225,225,123),
	       c(226,226,122),
	       c(226,226,121),
	       c(227,227,119),
	       c(228,228,118),
	       c(229,229,117),
	       c(229,229,116),
	       c(230,230,114),
	       c(231,231,113),
	       c(232,232,112),
	       c(232,232,110),
	       c(233,233,109),
	       c(234,234,107),
	       c(234,234,106),
	       c(235,235,104),
	       c(236,236,103),
	       c(237,237,101),
	       c(237,237,100),
	       c(238,238,98),
	       c(239,239,96),
	       c(239,239,94),
	       c(240,240,92),
	       c(241,241,91),
	       c(242,242,89),
	       c(242,242,86),
	       c(243,243,84),
	       c(244,244,82),
	       c(245,245,80),
	       c(245,245,77),
	       c(246,246,74),
	       c(247,247,72),
	       c(247,247,69),
	       c(248,248,65),
	       c(249,249,62),
	       c(250,250,58),
	       c(250,250,54),
	       c(251,251,49),
	       c(252,252,44),
	       c(253,253,37),
	       c(253,253,28),
	       c(254,254,13))[ round(seq(beg,end, length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)
}
heat.ob <- function(n,beg = 1,end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(0, 0, 0),
	       c(35, 0, 0),
	       c(52, 0, 0),
	       c(60, 0, 0),
	       c(63, 1, 0),
	       c(64, 2, 0),
	       c(68, 5, 0),
	       c(69, 6, 0),
	       c(72, 8, 0),
	       c(74,10, 0),
	       c(77,12, 0),
	       c(78,14, 0),
	       c(81,16, 0),
	       c(83,17, 0),
	       c(85,19, 0),
	       c(86,20, 0),
	       c(89,22, 0),
	       c(91,24, 0),
	       c(92,25, 0),
	       c(94,26, 0),
	       c(95,28, 0),
	       c(98,30, 0),
	       c(100,31, 0),
	       c(102,33, 0),
	       c(103,34, 0),
	       c(105,35, 0),
	       c(106,36, 0),
	       c(108,38, 0),
	       c(109,39, 0),
	       c(111,40, 0),
	       c(112,42, 0),
	       c(114,43, 0),
	       c(115,44, 0),
	       c(117,45, 0),
	       c(119,47, 0),
	       c(119,47, 0),
	       c(120,48, 0),
	       c(122,49, 0),
	       c(123,51, 0),
	       c(125,52, 0),
	       c(125,52, 0),
	       c(126,53, 0),
	       c(128,54, 0),
	       c(129,56, 0),
	       c(129,56, 0),
	       c(131,57, 0),
	       c(132,58, 0),
	       c(134,59, 0),
	       c(134,59, 0),
	       c(136,61, 0),
	       c(137,62, 0),
	       c(137,62, 0),
	       c(139,63, 0),
	       c(139,63, 0),
	       c(140,65, 0),
	       c(142,66, 0),
	       c(142,66, 0),
	       c(143,67, 0),
	       c(143,67, 0),
	       c(145,68, 0),
	       c(145,68, 0),
	       c(146,70, 0),
	       c(146,70, 0),
	       c(148,71, 0),
	       c(148,71, 0),
	       c(149,72, 0),
	       c(149,72, 0),
	       c(151,73, 0),
	       c(151,73, 0),
	       c(153,75, 0),
	       c(153,75, 0),
	       c(154,76, 0),
	       c(154,76, 0),
	       c(154,76, 0),
	       c(156,77, 0),
	       c(156,77, 0),
	       c(157,79, 0),
	       c(157,79, 0),
	       c(159,80, 0),
	       c(159,80, 0),
	       c(159,80, 0),
	       c(160,81, 0),
	       c(160,81, 0),
	       c(162,82, 0),
	       c(162,82, 0),
	       c(163,84, 0),
	       c(163,84, 0),
	       c(165,85, 0),
	       c(165,85, 0),
	       c(166,86, 0),
	       c(166,86, 0),
	       c(166,86, 0),
	       c(168,87, 0),
	       c(168,87, 0),
	       c(170,89, 0),
	       c(170,89, 0),
	       c(171,90, 0),
	       c(171,90, 0),
	       c(173,91, 0),
	       c(173,91, 0),
	       c(174,93, 0),
	       c(174,93, 0),
	       c(176,94, 0),
	       c(176,94, 0),
	       c(177,95, 0),
	       c(177,95, 0),
	       c(179,96, 0),
	       c(179,96, 0),
	       c(180,98, 0),
	       c(182,99, 0),
	       c(182,99, 0),
	       c(183,100, 0),
	       c(183,100, 0),
	       c(185,102, 0),
	       c(185,102, 0),
	       c(187,103, 0),
	       c(187,103, 0),
	       c(188,104, 0),
	       c(188,104, 0),
	       c(190,105, 0),
	       c(191,107, 0),
	       c(191,107, 0),
	       c(193,108, 0),
	       c(193,108, 0),
	       c(194,109, 0),
	       c(196,110, 0),
	       c(196,110, 0),
	       c(197,112, 0),
	       c(197,112, 0),
	       c(199,113, 0),
	       c(200,114, 0),
	       c(200,114, 0),
	       c(202,116, 0),
	       c(202,116, 0),
	       c(204,117, 0),
	       c(205,118, 0),
	       c(205,118, 0),
	       c(207,119, 0),
	       c(208,121, 0),
	       c(208,121, 0),
	       c(210,122, 0),
	       c(211,123, 0),
	       c(211,123, 0),
	       c(213,124, 0),
	       c(214,126, 0),
	       c(214,126, 0),
	       c(216,127, 0),
	       c(217,128, 0),
	       c(217,128, 0),
	       c(219,130, 0),
	       c(221,131, 0),
	       c(221,131, 0),
	       c(222,132, 0),
	       c(224,133, 0),
	       c(224,133, 0),
	       c(225,135, 0),
	       c(227,136, 0),
	       c(227,136, 0),
	       c(228,137, 0),
	       c(230,138, 0),
	       c(230,138, 0),
	       c(231,140, 0),
	       c(233,141, 0),
	       c(233,141, 0),
	       c(234,142, 0),
	       c(236,144, 0),
	       c(236,144, 0),
	       c(238,145, 0),
	       c(239,146, 0),
	       c(241,147, 0),
	       c(241,147, 0),
	       c(242,149, 0),
	       c(244,150, 0),
	       c(244,150, 0),
	       c(245,151, 0),
	       c(247,153, 0),
	       c(247,153, 0),
	       c(248,154, 0),
	       c(250,155, 0),
	       c(251,156, 0),
	       c(251,156, 0),
	       c(253,158, 0),
	       c(255,159, 0),
	       c(255,159, 0),
	       c(255,160, 0),
	       c(255,161, 0),
	       c(255,163, 0),
	       c(255,163, 0),
	       c(255,164, 0),
	       c(255,165, 0),
	       c(255,167, 0),
	       c(255,167, 0),
	       c(255,168, 0),
	       c(255,169, 0),
	       c(255,169, 0),
	       c(255,170, 0),
	       c(255,172, 0),
	       c(255,173, 0),
	       c(255,173, 0),
	       c(255,174, 0),
	       c(255,175, 0),
	       c(255,177, 0),
	       c(255,178, 0),
	       c(255,179, 0),
	       c(255,181, 0),
	       c(255,181, 0),
	       c(255,182, 0),
	       c(255,183, 0),
	       c(255,184, 0),
	       c(255,187, 7),
	       c(255,188,10),
	       c(255,189,14),
	       c(255,191,18),
	       c(255,192,21),
	       c(255,193,25),
	       c(255,195,29),
	       c(255,197,36),
	       c(255,198,40),
	       c(255,200,43),
	       c(255,202,51),
	       c(255,204,54),
	       c(255,206,61),
	       c(255,207,65),
	       c(255,210,72),
	       c(255,211,76),
	       c(255,214,83),
	       c(255,216,91),
	       c(255,219,98),
	       c(255,221,105),
	       c(255,223,109),
	       c(255,225,116),
	       c(255,228,123),
	       c(255,232,134),
	       c(255,234,142),
	       c(255,237,149),
	       c(255,239,156),
	       c(255,240,160),
	       c(255,243,167),
	       c(255,246,174),
	       c(255,248,182),
	       c(255,249,185),
	       c(255,252,193),
	       c(255,253,196),
	       c(255,255,204),
	       c(255,255,207),
	       c(255,255,211),
	       c(255,255,218),
	       c(255,255,222),
	       c(255,255,225),
	       c(255,255,229),
	       c(255,255,233),
	       c(255,255,236),
	       c(255,255,240),
	       c(255,255,244),
	       c(255,255,247),
	       c(255,255,255))[ round(seq(beg,end,length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)

}
LinGray <- function(n,beg = 1,end = 92)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(0,0,0),
	       c(0,0,0),
	       c(1,1,1),
	       c(1,1,1),
	       c(2,2,2),
	       c(3,3,3),
	       c(4,4,4),
	       c(5,5,5),
	       c(6,6,6),
	       c(7,7,7),
	       c(8,8,8),
	       c(9,9,9),
	       c(10,10,10),
	       c(11,11,11),
	       c(12,12,12),
	       c(13,13,13),
	       c(14,14,14),
	       c(15,15,15),
	       c(16,16,16),
	       c(17,17,17),
	       c(18,18,18),
	       c(19,19,19),
	       c(20,20,20),
	       c(21,21,21),
	       c(22,22,22),
	       c(23,23,23),
	       c(24,24,24),
	       c(25,25,25),
	       c(26,26,26),
	       c(27,27,27),
	       c(28,28,28),
	       c(29,29,29),
	       c(30,30,30),
	       c(32,32,32),
	       c(34,34,34),
	       c(35,35,35),
	       c(37,37,37),
	       c(39,39,39),
	       c(41,41,41),
	       c(43,43,43),
	       c(45,45,45),
	       c(46,46,46),
	       c(47,47,47),
	       c(49,49,49),
	       c(51,51,51),
	       c(52,52,52),
	       c(54,54,54),
	       c(56,56,56),
	       c(59,59,59),
	       c(61,61,61),
	       c(64,64,64),
	       c(67,67,67),
	       c(69,69,69),
	       c(72,72,72),
	       c(75,75,75),
	       c(76,76,76),
	       c(78,78,78),
	       c(81,81,81),
	       c(84,84,84),
	       c(87,87,87),
	       c(91,91,91),
	       c(94,94,94),
	       c(97,97,97),
	       c(101,101,101),
	       c(104,104,104),
	       c(107,107,107),
	       c(108,108,108),
	       c(112,112,112),
	       c(116,116,116),
	       c(120,120,120),
	       c(124,124,124),
	       c(128,128,128),
	       c(132,132,132),
	       c(136,136,136),
	       c(141,141,141),
	       c(145,145,145),
	       c(147,147,147),
	       c(150,150,150),
	       c(154,154,154),
	       c(159,159,159),
	       c(164,164,164),
	       c(169,169,169),
	       c(174,174,174),
	       c(179,179,179),
	       c(185,185,185),
	       c(190,190,190),
	       c(195,195,195),
	       c(201,201,201),
	       c(207,207,207),
	       c(212,212,212),
	       c(216,216,216),
	       c(218,218,218),
	       c(224,224,224),
	       c(226,226,226),
	       c(230,230,230),
	       c(237,237,237),
	       c(243,243,243),
	       c(245,245,245),
	       c(252,252,252),
	       c(255,255,255),
	       c(255,255,255))[round(seq(beg,end,length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)
}
LinOCS <- function(n,beg = 1,end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(0,0,0),
	       c(0,0,0),
	       c(0,0,0),
	       c(1,0,0),
	       c(2,0,0),
	       c(2,0,0),
	       c(3,0,0),
	       c(3,0,0),
	       c(4,0,0),
	       c(5,0,0),
	       c(5,0,0),
	       c(6,0,0),
	       c(7,0,0),
	       c(7,0,0),
	       c(8,0,0),
	       c(9,0,0),
	       c(9,0,0),
	       c(10,0,0),
	       c(11,0,0),
	       c(12,0,0),
	       c(13,0,0),
	       c(14,0,0),
	       c(15,0,0),
	       c(16,0,0),
	       c(17,0,0),
	       c(18,0,0),
	       c(19,0,0),
	       c(20,0,0),
	       c(21,0,0),
	       c(22,0,0),
	       c(23,0,0),
	       c(25,0,0),
	       c(26,0,0),
	       c(27,0,0),
	       c(28,0,0),
	       c(30,0,0),
	       c(31,0,0),
	       c(33,0,0),
	       c(34,0,0),
	       c(35,0,0),
	       c(37,0,0),
	       c(39,0,0),
	       c(40,0,0),
	       c(43,0,0),
	       c(45,0,0),
	       c(46,0,0),
	       c(49,0,0),
	       c(51,0,0),
	       c(53,0,0),
	       c(54,0,0),
	       c(56,0,0),
	       c(58,0,0),
	       c(60,0,0),
	       c(62,0,0),
	       c(64,0,0),
	       c(67,0,0),
	       c(69,0,0),
	       c(71,0,0),
	       c(74,0,0),
	       c(76,0,0),
	       c(80,0,0),
	       c(81,0,0),
	       c(84,0,0),
	       c(86,0,0),
	       c(89,0,0),
	       c(92,0,0),
	       c(94,0,0),
	       c(97,0,0),
	       c(100,0,0),
	       c(103,0,0),
	       c(106,0,0),
	       c(109,0,0),
	       c(112,0,0),
	       c(115,0,0),
	       c(117,0,0),
	       c(122,0,0),
	       c(126,0,0),
	       c(128,0,0),
	       c(131,0,0),
	       c(135,0,0),
	       c(135,0,0),
	       c(135,1,0),
	       c(135,2,0),
	       c(135,3,0),
	       c(135,4,0),
	       c(135,6,0),
	       c(135,6,0),
	       c(135,8,0),
	       c(135,9,0),
	       c(135,10,0),
	       c(135,11,0),
	       c(135,13,0),
	       c(135,13,0),
	       c(135,15,0),
	       c(135,17,0),
	       c(135,17,0),
	       c(135,19,0),
	       c(135,21,0),
	       c(135,22,0),
	       c(135,23,0),
	       c(135,25,0),
	       c(135,26,0),
	       c(135,27,0),
	       c(135,29,0),
	       c(135,31,0),
	       c(135,32,0),
	       c(135,33,0),
	       c(135,35,0),
	       c(135,36,0),
	       c(135,38,0),
	       c(135,40,0),
	       c(135,42,0),
	       c(135,44,0),
	       c(135,46,0),
	       c(135,47,0),
	       c(135,49,0),
	       c(135,51,0),
	       c(135,52,0),
	       c(135,54,0),
	       c(135,56,0),
	       c(135,57,0),
	       c(135,59,0),
	       c(135,62,0),
	       c(135,63,0),
	       c(135,65,0),
	       c(135,67,0),
	       c(135,69,0),
	       c(135,72,0),
	       c(135,73,0),
	       c(135,76,0),
	       c(135,78,0),
	       c(135,80,0),
	       c(135,82,0),
	       c(135,84,0),
	       c(135,87,0),
	       c(135,88,0),
	       c(135,90,0),
	       c(135,93,0),
	       c(135,95,0),
	       c(135,98,0),
	       c(135,101,0),
	       c(135,103,0),
	       c(135,106,0),
	       c(135,107,0),
	       c(135,110,0),
	       c(135,113,0),
	       c(135,115,0),
	       c(135,118,0),
	       c(135,121,0),
	       c(135,124,0),
	       c(135,127,0),
	       c(135,129,0),
	       c(135,133,0),
	       c(135,135,0),
	       c(135,138,0),
	       c(135,141,0),
	       c(135,144,0),
	       c(135,148,0),
	       c(135,150,0),
	       c(135,155,0),
	       c(135,157,0),
	       c(135,160,0),
	       c(135,163,0),
	       c(135,166,0),
	       c(135,170,0),
	       c(135,174,0),
	       c(135,177,0),
	       c(135,180,0),
	       c(135,184,0),
	       c(135,188,0),
	       c(135,192,0),
	       c(135,195,0),
	       c(135,200,0),
	       c(135,203,0),
	       c(135,205,0),
	       c(135,210,0),
	       c(135,214,0),
	       c(135,218,0),
	       c(135,222,0),
	       c(135,226,0),
	       c(135,231,0),
	       c(135,236,0),
	       c(135,239,0),
	       c(135,244,0),
	       c(135,249,0),
	       c(135,254,0),
	       c(135,255,1),
	       c(135,255,5),
	       c(135,255,10),
	       c(135,255,15),
	       c(135,255,20),
	       c(135,255,23),
	       c(135,255,28),
	       c(135,255,33),
	       c(135,255,38),
	       c(135,255,43),
	       c(135,255,45),
	       c(135,255,49),
	       c(135,255,54),
	       c(135,255,59),
	       c(135,255,65),
	       c(135,255,70),
	       c(135,255,74),
	       c(135,255,80),
	       c(135,255,84),
	       c(135,255,90),
	       c(135,255,95),
	       c(135,255,98),
	       c(135,255,104),
	       c(135,255,110),
	       c(135,255,116),
	       c(135,255,120),
	       c(135,255,125),
	       c(135,255,131),
	       c(135,255,137),
	       c(135,255,144),
	       c(135,255,149),
	       c(135,255,154),
	       c(135,255,158),
	       c(135,255,165),
	       c(135,255,172),
	       c(135,255,179),
	       c(135,255,186),
	       c(135,255,191),
	       c(135,255,198),
	       c(135,255,203),
	       c(135,255,211),
	       c(135,255,216),
	       c(135,255,224),
	       c(135,255,232),
	       c(135,255,240),
	       c(135,255,248),
	       c(135,255,254),
	       c(135,255,255),
	       c(140,255,255),
	       c(146,255,255),
	       c(153,255,255),
	       c(156,255,255),
	       c(161,255,255),
	       c(168,255,255),
	       c(172,255,255),
	       c(177,255,255),
	       c(182,255,255),
	       c(189,255,255),
	       c(192,255,255),
	       c(199,255,255),
	       c(204,255,255),
	       c(210,255,255),
	       c(215,255,255),
	       c(220,255,255),
	       c(225,255,255),
	       c(232,255,255),
	       c(236,255,255),
	       c(240,255,255),
	       c(248,255,255),
	       c(255,255,255))[ round(seq(beg,end,length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)
}
magent <- function(n, beg = 1, end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
	stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(0,  0,	 0),
	       c( 40,  0,  0),
	       c( 56,  0,  4),
	       c( 61,  0,  9),
	       c( 64,  0, 12),
	       c( 66,  0, 14),
	       c( 69,  0, 17),
	       c( 73,  0, 20),
	       c( 74,  0, 22),
	       c( 78,  0, 25),
	       c( 79,  0, 27),
	       c( 83,  0, 30),
	       c( 85,  0, 31),
	       c( 86,  0, 33),
	       c( 90,  0, 36),
	       c( 91,  0, 38),
	       c( 93,  0, 39),
	       c( 95,  0, 41),
	       c( 96,  0, 43),
	       c(100,  0, 46),
	       c(102,  0, 47),
	       c(103,  0, 49),
	       c(105,  0, 51),
	       c(107,  0, 52),
	       c(108,  0, 54),
	       c(110,  0, 55),
	       c(112,  0, 57),
	       c(112,  0, 57),
	       c(113,  0, 58),
	       c(115,  0, 60),
	       c(117,  0, 62),
	       c(119,  0, 63),
	       c(120,  0, 65),
	       c(122,  0, 66),
	       c(124,  0, 68),
	       c(125,  0, 70),
	       c(127,  0, 71),
	       c(129,  0, 73),
	       c(129,  0, 73),
	       c(130,  0, 74),
	       c(132,  0, 76),
	       c(134,  0, 78),
	       c(136,  0, 79),
	       c(137,  0, 81),
	       c(139,  0, 82),
	       c(141,  0, 84),
	       c(142,  0, 86),
	       c(144,  0, 87),
	       c(146,  0, 89),
	       c(147,  0, 90),
	       c(149,  0, 92),
	       c(151,  0, 94),
	       c(151,  0, 94),
	       c(153,  0, 95),
	       c(154,  0, 97),
	       c(156,  0, 98),
	       c(158,  0,100),
	       c(159,  0,102),
	       c(161,  0,103),
	       c(163,  0,105),
	       c(164,  0,106),
	       c(166,  0,108),
	       c(168,  0,109),
	       c(170,  0,111),
	       c(171,  0,113),
	       c(173,  0,114),
	       c(175,  0,116),
	       c(176,  0,117),
	       c(178,  0,119),
	       c(180,  0,121),
	       c(180,  0,121),
	       c(181,  0,122),
	       c(183,  0,124),
	       c(185,  0,125),
	       c(187,  0,127),
	       c(188,  0,129),
	       c(190,  0,130),
	       c(192,  0,132),
	       c(193,  0,133),
	       c(195,  0,135),
	       c(197,  0,137),
	       c(198,  0,138),
	       c(200,  0,140),
	       c(202,  0,141),
	       c(204,  0,143),
	       c(204,  0,143),
	       c(205,  0,145),
	       c(207,  0,146),
	       c(209,  0,148),
	       c(210,  0,149),
	       c(212,  0,151),
	       c(214,  0,153),
	       c(215,  0,154),
	       c(217,  0,156),
	       c(219,  0,157),
	       c(221,  0,159),
	       c(222,  0,160),
	       c(222,  0,160),
	       c(224,  0,162),
	       c(226,  0,164),
	       c(227,  0,165),
	       c(229,  0,167),
	       c(231,  0,168),
	       c(232,  0,170),
	       c(234,  0,172),
	       c(236,  0,173),
	       c(238,  0,175),
	       c(238,  0,175),
	       c(239,  0,176),
	       c(241,  0,178),
	       c(243,  0,180),
	       c(244,  0,181),
	       c(246,  0,183),
	       c(248,  2,184),
	       c(249,  4,186),
	       c(249,  4,186),
	       c(249,  4,186),
	       c(251,  6,188),
	       c(251,  6,188),
	       c(253,  9,189),
	       c(253,  9,189),
	       c(255, 11,191),
	       c(255, 11,191),
	       c(255, 13,192),
	       c(255, 13,192),
	       c(255, 13,192),
	       c(255, 16,194),
	       c(255, 18,196),
	       c(255, 20,197),
	       c(255, 20,197),
	       c(255, 23,199),
	       c(255, 25,200),
	       c(255, 27,202),
	       c(255, 30,204),
	       c(255, 32,205),
	       c(255, 34,207),
	       c(255, 37,208),
	       c(255, 37,208),
	       c(255, 39,210),
	       c(255, 41,211),
	       c(255, 44,213),
	       c(255, 46,215),
	       c(255, 48,216),
	       c(255, 51,218),
	       c(255, 53,219),
	       c(255, 53,219),
	       c(255, 55,221),
	       c(255, 57,223),
	       c(255, 60,224),
	       c(255, 62,226),
	       c(255, 64,227),
	       c(255, 67,229),
	       c(255, 67,229),
	       c(255, 69,231),
	       c(255, 71,232),
	       c(255, 74,234),
	       c(255, 76,235),
	       c(255, 78,237),
	       c(255, 81,239),
	       c(255, 81,239),
	       c(255, 83,240),
	       c(255, 85,242),
	       c(255, 88,243),
	       c(255, 90,245),
	       c(255, 92,247),
	       c(255, 95,248),
	       c(255, 95,248),
	       c(255, 97,250),
	       c(255, 99,251),
	       c(255,102,253),
	       c(255,104,255),
	       c(255,106,255),
	       c(255,106,255),
	       c(255,108,255),
	       c(255,111,255),
	       c(255,113,255),
	       c(255,115,255),
	       c(255,115,255),
	       c(255,118,255),
	       c(255,120,255),
	       c(255,122,255),
	       c(255,122,255),
	       c(255,125,255),
	       c(255,127,255),
	       c(255,129,255),
	       c(255,129,255),
	       c(255,132,255),
	       c(255,134,255),
	       c(255,136,255),
	       c(255,136,255),
	       c(255,139,255),
	       c(255,141,255),
	       c(255,143,255),
	       c(255,143,255),
	       c(255,146,255),
	       c(255,148,255),
	       c(255,150,255),
	       c(255,150,255),
	       c(255,153,255),
	       c(255,155,255),
	       c(255,155,255),
	       c(255,157,255),
	       c(255,159,255),
	       c(255,159,255),
	       c(255,162,255),
	       c(255,164,255),
	       c(255,164,255),
	       c(255,166,255),
	       c(255,169,255),
	       c(255,171,255),
	       c(255,171,255),
	       c(255,173,255),
	       c(255,176,255),
	       c(255,176,255),
	       c(255,178,255),
	       c(255,180,255),
	       c(255,180,255),
	       c(255,183,255),
	       c(255,185,255),
	       c(255,185,255),
	       c(255,187,255),
	       c(255,190,255),
	       c(255,190,255),
	       c(255,192,255),
	       c(255,194,255),
	       c(255,197,255),
	       c(255,197,255),
	       c(255,199,255),
	       c(255,201,255),
	       c(255,204,255),
	       c(255,204,255),
	       c(255,206,255),
	       c(255,208,255),
	       c(255,210,255),
	       c(255,210,255),
	       c(255,213,255),
	       c(255,215,255),
	       c(255,217,255),
	       c(255,217,255),
	       c(255,220,255),
	       c(255,222,255),
	       c(255,224,255),
	       c(255,227,255),
	       c(255,229,255),
	       c(255,229,255),
	       c(255,231,255),
	       c(255,234,255),
	       c(255,236,255),
	       c(255,238,255),
	       c(255,241,255),
	       c(255,243,255),
	       c(255,243,255),
	       c(255,245,255),
	       c(255,248,255),
	       c(255,250,255),
	       c(255,255,255)) [ round(seq(beg,end,length = n)), ]

    rgb(M[,1]/255,
	M[,2]/255,
	M[,3]/255)
}
plinrain <- function(n, beg = 1, end = 256)
{
    if(beg < 1 || end < 1 || beg > 256 || end > 256)
        stop("`beg' and `end' must be numbers in the interval [1,256]")

    M <- rbind(c(  0,  0,  0),
               c( 45,  0, 36),
               c( 56,  0, 46),
               c( 60,  0, 49),
               c( 67,  0, 54),
               c( 70,  0, 59),
               c( 71,  0, 61),
               c( 75,  0, 68),
               c( 74,  0, 73),
               c( 74,  0, 77),
               c( 73,  0, 81),
               c( 71,  0, 87),
               c( 69,  1, 90),
               c( 68,  2, 94),
               c( 66,  3, 97),
               c( 63,  6,102),
               c( 61,  7,106),
               c( 58, 10,109),
               c( 56, 12,113),
               c( 53, 15,116),
               c( 48, 18,119),
               c( 47, 20,121),
               c( 44, 23,124),
               c( 41, 27,128),
               c( 40, 28,129),
               c( 37, 32,132),
               c( 34, 36,134),
               c( 29, 43,137),
               c( 25, 52,138),
               c( 24, 57,139),
               c( 24, 62,141),
               c( 24, 64,142),
               c( 23, 65,142),
               c( 23, 69,143),
               c( 23, 71,142),
               c( 23, 71,142),
               c( 23, 73,142),
               c( 23, 75,142),
               c( 23, 75,142),
               c( 23, 78,142),
               c( 23, 80,142),
               c( 23, 80,142),
               c( 23, 82,141),
               c( 23, 85,141),
               c( 23, 85,141),
               c( 23, 87,140),
               c( 23, 87,140),
               c( 24, 90,140),
               c( 24, 90,140),
               c( 24, 93,139),
               c( 24, 93,139),
               c( 24, 93,139),
               c( 24, 93,139),
               c( 24, 97,139),
               c( 24, 97,139),
               c( 25,101,138),
               c( 25,101,138),
               c( 25,104,137),
               c( 25,104,137),
               c( 25,104,137),
               c( 26,108,137),
               c( 26,108,137),
               c( 27,111,136),
               c( 27,111,136),
               c( 27,111,136),
               c( 27,115,135),
               c( 27,115,135),
               c( 28,118,134),
               c( 28,118,134),
               c( 29,122,133),
               c( 29,122,133),
               c( 29,122,133),
               c( 29,122,133),
               c( 29,125,132),
               c( 29,125,132),
               c( 30,128,131),
               c( 30,128,131),
               c( 31,131,130),
               c( 31,131,130),
               c( 31,131,130),
               c( 32,134,128),
               c( 32,134,128),
               c( 33,137,127),
               c( 33,137,127),
               c( 33,137,127),
               c( 34,140,125),
               c( 34,140,125),
               c( 35,142,123),
               c( 35,142,123),
               c( 36,145,121),
               c( 36,145,121),
               c( 36,145,121),
               c( 37,147,118),
               c( 37,147,118),
               c( 38,150,116),
               c( 38,150,116),
               c( 40,152,113),
               c( 40,152,113),
               c( 41,154,111),
               c( 41,154,111),
               c( 42,156,108),
               c( 42,156,108),
               c( 43,158,106),
               c( 43,158,106),
               c( 43,158,106),
               c( 45,160,104),
               c( 45,160,104),
               c( 46,162,101),
               c( 46,162,101),
               c( 48,164, 99),
               c( 48,164, 99),
               c( 50,166, 97),
               c( 50,166, 97),
               c( 51,168, 95),
               c( 53,170, 93),
               c( 53,170, 93),
               c( 53,170, 93),
               c( 55,172, 91),
               c( 55,172, 91),
               c( 57,174, 88),
               c( 57,174, 88),
               c( 59,175, 86),
               c( 62,177, 84),
               c( 64,178, 82),
               c( 64,178, 82),
               c( 67,180, 80),
               c( 67,180, 80),
               c( 69,181, 79),
               c( 72,183, 77),
               c( 72,183, 77),
               c( 72,183, 77),
               c( 75,184, 76),
               c( 77,186, 74),
               c( 80,187, 73),
               c( 83,189, 72),
               c( 87,190, 72),
               c( 91,191, 71),
               c( 95,192, 70),
               c( 99,193, 70),
               c(103,194, 70),
               c(107,195, 70),
               c(111,196, 70),
               c(111,196, 70),
               c(115,196, 70),
               c(119,197, 70),
               c(123,197, 70),
               c(130,198, 71),
               c(133,199, 71),
               c(137,199, 72),
               c(140,199, 72),
               c(143,199, 73),
               c(143,199, 73),
               c(147,199, 73),
               c(150,199, 74),
               c(153,199, 74),
               c(156,199, 75),
               c(160,200, 76),
               c(167,200, 78),
               c(170,200, 79),
               c(173,200, 79),
               c(173,200, 79),
               c(177,200, 80),
               c(180,200, 81),
               c(183,199, 82),
               c(186,199, 82),
               c(190,199, 83),
               c(196,199, 85),
               c(199,198, 85),
               c(199,198, 85),
               c(203,198, 86),
               c(206,197, 87),
               c(212,197, 89),
               c(215,196, 90),
               c(218,195, 91),
               c(224,194, 94),
               c(224,194, 94),
               c(230,193, 96),
               c(233,192, 98),
               c(236,190,100),
               c(238,189,104),
               c(240,188,106),
               c(240,188,106),
               c(242,187,110),
               c(244,185,114),
               c(245,184,116),
               c(247,183,120),
               c(248,182,123),
               c(248,182,123),
               c(250,181,125),
               c(251,180,128),
               c(252,180,130),
               c(253,180,133),
               c(253,180,133),
               c(254,180,134),
               c(254,179,138),
               c(255,179,142),
               c(255,179,145),
               c(255,179,145),
               c(255,179,152),
               c(255,180,161),
               c(255,180,164),
               c(255,180,167),
               c(255,180,167),
               c(255,181,169),
               c(255,181,170),
               c(255,182,173),
               c(255,183,176),
               c(255,183,176),
               c(255,184,179),
               c(255,185,179),
               c(255,185,182),
               c(255,186,182),
               c(255,186,182),
               c(255,187,185),
               c(255,188,185),
               c(255,189,188),
               c(255,189,188),
               c(255,190,188),
               c(255,191,191),
               c(255,192,191),
               c(255,194,194),
               c(255,194,194),
               c(255,197,197),
               c(255,198,198),
               c(255,200,200),
               c(255,201,201),
               c(255,201,201),
               c(255,202,202),
               c(255,203,203),
               c(255,205,205),
               c(255,206,206),
               c(255,206,206),
               c(255,208,208),
               c(255,209,209),
               c(255,211,211),
               c(255,215,215),
               c(255,216,216),
               c(255,216,216),
               c(255,218,218),
               c(255,219,219),
               c(255,221,221),
               c(255,223,223),
               c(255,226,226),
               c(255,228,228),
               c(255,230,230),
               c(255,230,230),
               c(255,232,232),
               c(255,235,235),
               c(255,237,237),
               c(255,240,240),
               c(255,243,243),
               c(255,246,246),
               c(255,249,249),
               c(255,251,251),
               c(255,253,253),
               c(255,255,255))[ round(seq(beg,end, length = n)), ]

    rgb(M[,1]/255,
        M[,2]/255,
        M[,3]/255)
}
hexbin <-
    function(x, y, xbins = 30, shape = 1, xbnds = range(x), ybnds = range(y))
{
    call<-match.call()
    n <- length(x)
    if(length(y) != n)
        stop("Vector length mismatch")
    if(diff(xbnds) == 0)
        stop("x range must be positive")
    if(diff(ybnds) == 0)
        stop("y range must be positive")
    jmax <- floor(xbins + 1.5001)
    imax <- 2 * floor((xbins * shape)/sqrt(3) + 1.5001)
    dim <- c(imax, jmax)
    lmax <- jmax * imax
    ans <- .Fortran("hbin",
                    x = as.double(x),
                    y = as.double(y),
                    cell= integer(lmax),
                    cnt = integer(lmax),
                    xcm = double(lmax),
                    ycm = double(lmax),
                    xbins = as.double(xbins),
                    shape = as.double(shape),
                    xbnds = as.double(xbnds),
                    ybnds = as.double(ybnds),
                    dim = as.integer(dim),
                    n = as.integer(n),
                    PACKAGE = "hexbin")[-(1:2)]
    ## delele data and extraneous stuff
    nc <- ans$n
    length(ans$cell) <- nc
    length(ans$cnt) <- nc
    length(ans$xcm) <- nc
    length(ans$ycm) <- nc
    if(sum(ans$cnt) != n) warning("Lost counts in binning")
    ans$call <- call
    class(ans) <- "hexbin"
    ans
}## hexbin

hboxplot <- function(bin1, xbnds = bin1$xbnds, ybnds = bin1$ybnds,
                     density, border = c(FALSE, FALSE),
                     pen = c(2, 3), unzoom = 1.04, reshape = FALSE,
                     xlab = "", ylab = "", ...)
{
    ## Arguments:
    ##	bin1 = hexagon bin object
    ## 	density=    for hexagon graph paper
    ## 	border      plot the border of the hexagon, use T for
    ##                   hexagon graph paper

### FIXME: This is (almost?) identical to first part of hdiffplot()

    ##_______________ Collect computing constants______________
    tmp1 <- hcell2xy(bin1)
    fix <- missing(xbnds)
    if(fix) {
        xbnds <- bin1$xbnds
        ybnds <- bin1$ybnds
        nxbnds <- range(tmp1$x)
        nybnds <- range(tmp1$y)
    }
    else { ## !fix
        if(missing(ybnds)) ## FIXME: this just return()ed
            ## MM thinks even warning() is too much -- rather check consistency
            warning("`xbnds' specified;  `ybnds' not...")
        nxbnds <- xbnds
        nybnds <- ybnds
    }
    if(!missing(density)) warning("`density' is not used (rather color)")
    if(fix || reshape) {
        shape <- bin1$shape

        opar <- par(c("pin","mai"))
        on.exit(par(opar))
        pin <- opar$pin
        mai <- opar$mai
        xsize <- pin[1]
        ysize <- pin[2]
        if(shape * xsize <= ysize) {
            inc <- (ysize - shape * xsize)/2
            mai[1] <- mai[1] + inc
            mai[3] <- mai[3] + inc
            ysize <- shape * xsize
        }
        else {
            inc <- (xsize - ysize/shape)/2
            mai[2] <- mai[2] + inc
            mai[4] <- mai[4] + inc
            xsize <- ysize/shape
        }
        par(mai = mai)
    }
    ##___zoom in scaling with expanding to avoid hexagons outside plot frame___
    if(fix) {
        ratiox <- diff(nxbnds)/diff(xbnds)
        ratioy <- diff(nybnds)/diff(ybnds)
        ratio <- max(ratioy, ratiox)
        nxbnds <- mean(nxbnds) + c(-1,1)*(unzoom * ratio * diff(xbnds))/2
        nybnds <- mean(nybnds) + c(-1,1)*(unzoom * ratio * diff(ybnds))/2
    }
    #browser()
    plot(nxbnds, nybnds, type = "n", xlab = xlab, ylab = ylab, ...)
    cnt <- bin1$cnt
    xbins <- bin1$xbins
    shape <- bin1$shape
    xnew <- tmp1$x
    ynew <- tmp1$y
    ##__________________ Construct a hexagon___________________
    dx <- (0.5 * diff(bin1$xbnds))/xbins
    dy <- (0.5 * diff(bin1$ybnds))/(xbins * shape * sqrt(3))
    polyx <- c(dx, dx, 0,  - dx,  - dx, 0, NA)
    polyy <- c(dy,  - dy, -2 * dy,  - dy, dy, 2 * dy, NA)
    ##_______________ Full Cell Plotting_____________________
    n <- length(xnew)
    seven <- rep(7, n)
    pltx <- rep(polyx, n) + rep(xnew, seven)
    plty <- rep(polyy, n) + rep(ynew, seven)
    ##polygon(pltx, plty, density = density[2], border = border[2],col=pen[2])
    polygon(pltx, plty, border = border[2], col = pen[2])
    ##______________Plotting medians___________________________
    if(is.null(bin1$erode)) {
        warning("No erode component")
    }
    else {
        med <- bin1$erode == max(bin1$erode)
        xnew <- xnew[med]
        ynew <- ynew[med]
        n <- length(xnew)
        seven <- rep(7, n)
        pltx <- rep(polyx, n) + rep(xnew, seven)
        plty <- rep(polyy, n) + rep(ynew, seven)
        polygon(pltx, plty, border = border[1], col = pen[1])
    }
    ##_____________reset graphics_________________________________
    if(fix || reshape) {
        on.exit()
        invisible( par(opar) )
    }
    else "done"
}# hboxplot()

hcell <- function(x, y, xbins = 30, shape = 1,
                  xbnds = range(x), ybnds = range(y),
                  verbose = getOption("verbose"))
{
    n <- length(x)
    if(length(y) != n)
        stop("Vector length mismatch")
    if(diff(xbnds) == 0)
        stop("No x range")
    if(diff(ybnds) == 0)
        stop("No y range")
    jmax <- floor(xbins + 1.5001)
    imax <- 2 * floor((xbins * shape)/sqrt(3) + 1.5001)
    dim <- c(imax, jmax)
    if(verbose) cat("OK before calling .Fortran(\"hcell\",*)\n")
    .Fortran("hcell",
             x = as.double(x),
             y = as.double(y),
             cell = integer(n),
             n = as.integer(n),
             xbins = as.double(xbins),
             shape = as.double(shape),
             xbnds = as.double(xbnds),
             ybnds = as.double(ybnds),
             dim = as.integer(dim),
             PACKAGE = "hexbin")[
             c("cell","n","xbins", "shape", "xbnds", "ybnds")]
}

hcell2xy <- function(bin)
{
    xbins <- bin$xbins
    xbnds <- bin$xbnds
    c3 <- diff(xbnds)/xbins
    ybnds <- bin$ybnds
    c4 <- (diff(ybnds) * sqrt(3))/(2 * bin$shape * xbins)
    jmax <- bin$dim[2]
    cell <- bin$cell - 1
    i <- cell %/% jmax
    j <- cell %% jmax
    y <- c4 * i + ybnds[1]
    x <- c3 * ifelse(i %% 2 == 0, j, j + 0.5) + xbnds[1]
    list(x = x, y = y)
}

### FIXME : Use S4 methods {i.e. library(methods) } to define
### -----   an `hexagon bin' class -- use at least is(*,) on these

hdiffplot <- function(bin1, bin2, xbnds, ybnds, density,
                      border = rep(FALSE, 6), pen = 2:7, size = 0.1, lwd = 2,
                      eps = 1e-6, unzoom = 1.04, xlab = "", ylab = "",
                      main = deparse(mycall), ...)
{
    ## Arguments:
    ##	bin1	: hexagon bin object
    ##  bin2	: hexagon bin object
    ##            bin objects must have the same plotting bounds and shape
    ## 	density : for hexagon graph paper
    ## 	border  : plot the border of the hexagon, use T for
    ##            hexagon graph paper

    ##_______________ Collect computing constants______________
    mycall <- sys.call()
    if(length(mycall) >= 4) {
        mycall[4] <- as.call(quote(.....()))
        if(length(mycall) > 4) mycall <- mycall[1:4]
    }
    tmp1 <- hcell2xy(bin1)
    tmp2 <- hcell2xy(bin2)
    if(!missing(density)) warning("`density' is not used (rather color)")
    fix <- missing(xbnds)
    if(fix) {
        xbnds <- bin1$xbnds
        ybnds <- bin1$ybnds
        nxbnds <- range(tmp1$x, tmp2$x)
        nybnds <- range(tmp1$y, tmp2$y)
        shape <- bin1$shape
        opar <- par(c("pin","mai"))
        on.exit(par(opar))
        pin <- opar$pin
        mai <- opar$mai
        xsize <- pin[1]
        ysize <- pin[2]
        if(shape * xsize <= ysize) {
            inc <- (ysize - shape * xsize)/2
            mai[1] <- mai[1] + inc
            mai[3] <- mai[3] + inc
            ysize <- shape * xsize
        }
        else {
            inc <- (xsize - ysize/shape)/2
            mai[2] <- mai[2] + inc
            mai[4] <- mai[4] + inc
            xsize <- ysize/shape
        }
        par(mai = mai)
        ##_zoom in scaling with expanding to avoid hexagons outside plot frame__
        ratiox <- diff(nxbnds)/diff(xbnds)
        ratioy <- diff(nybnds)/diff(ybnds)
        ratio <- max(ratioy, ratiox)

        nxbnds <- mean(nxbnds) + c(-1,1)*(unzoom * ratio * diff(xbnds))/2
        nybnds <- mean(nybnds) + c(-1,1)*(unzoom * ratio * diff(ybnds))/2

    }
    else { ## !fix
        if(missing(ybnds)) ## FIXME: this just return()ed
            ## MM thinks even warning() is too much -- rather check consistency
            warning("`xbnds' specified;  `ybnds' not...")
        nxbnds <- xbnds
        nybnds <- ybnds
    }
    plot(nxbnds, nybnds, type = "n", main = main, xlab = xlab, ylab = ylab, ...)
    ##__________________ Construct hexagon___________________
    xbins <- bin1$xbins
    shape <- bin1$shape
    dx <- (0.5 * diff(bin1$xbnds))/xbins
    dy <- (0.5 * diff(bin1$ybnds))/(xbins * shape * sqrt(3))
    polyx <- c(dx, dx,   0,  -dx,-dx,  0,  NA)
    polyy <- c(dy,-dy, -2*dy,-dy, dy, 2*dy,NA)
    ##_______________ Full Cell Plotting for Unique Bin1 Cells_________________
    sub1 <- match(bin1$cell, bin2$cell)
    na1 <- is.na(sub1)
    if(any(na1)) {
        xnew <- tmp1$x[na1]
        ynew <- tmp1$y[na1]
        n <- length(xnew)
        seven <- rep(7, n)
        pltx <- rep(polyx, n) + rep(xnew, seven)
        plty <- rep(polyy, n) + rep(ynew, seven)
        polygon(pltx, plty, border = border[1], col = pen[1])
    }
    ##_______________ Full Cell Plotting for Common Cells_____________________
    if(any(!na1)) {
        xnew <- tmp1$x[!na1]
        ynew <- tmp1$y[!na1]
        n <- length(xnew)
        seven <- rep(7, n)
        pltx <- rep(polyx, n) + rep(xnew, seven)
        plty <- rep(polyy, n) + rep(ynew, seven)
        polygon(pltx, plty, border = border[2], col = pen[2])
    }
    ##_______________ Full Cell Plotting for Unique Bin2 Cells_________________
    sub2 <- match(bin2$cell, bin1$cell)
    na2 <- is.na(sub2)
    if(any(na2)) {
        xnew <- tmp2$x[na2]
        ynew <- tmp2$y[na2]
        n <- length(xnew)
        seven <- rep(7, n)
        pltx <- rep(polyx, n) + rep(xnew, seven)
        plty <- rep(polyy, n) + rep(ynew, seven)
        polygon(pltx, plty, border = border[3], col = pen[3])
    }
    ##_____________________________Plot Median Cells___________________________
    if(!is.null(bin1$erode) && !is.null(bin2$erode)) {
        med1 <- bin1$erode == max(bin1$erode)
        xold <- tmp1$x[med1]
        yold <- tmp1$y[med1]
        xold <- xold[1]
        yold <- yold[1]
        med2 <- bin2$erode == max(bin2$erode)
        xnew <- tmp2$x[med2]
        ynew <- tmp2$y[med2]
        xnew <- xnew[1]
        ynew <- ynew[1]
        if(abs(xnew - xold) + abs(ynew - yold) > eps) {
            pltx <- polyx + rep(xold, 7)
            plty <- polyy + rep(yold, 7)
            polygon(pltx, plty,  border = border[4], col = pen[4])
            pltx <- polyx + rep(xnew, 7)
            plty <- polyy + rep(ynew, 7)
            polygon(pltx, plty, border = border[5], col = pen[5])
            arrows(xold, yold, xnew, ynew, length = size, lwd = lwd)
        }
        else {
            pltx <- polyx + rep(xold, 7)
            plty <- polyy + rep(yold, 7)
            polygon(pltx, plty, border = border[6], col = pen[6])
        }
    }
    ##________________Clean Up_______________________________________________
    if(fix) {
        on.exit()
        invisible( par(opar) )
    } else "Done"
} ## hdiffplot()

erode.hexbin <- function(bin, cdfcut = 0.5)
{
    if(!inherits(bin,"hexbin")) stop("first argument must be a hexbin object")
    bin.att<-attributes(bin)
    cell <- bin$cell
    cnt <- bin$cnt
    val <- rep(0, length(cell))
    tmp <- sort(cnt)
    cdf <- cumsum(tmp)/sum(cnt)
    good <- cdfcut <= cdf
    if(!any(good))
        return("no cells selected")
    crit <- min(tmp[good])
    good <- crit <= cnt
    cell <- cell[good]
    cnt <- cnt[good]
    bin$cell <- cell
    bin$cnt <- cnt
    n <- length(cell)
    bdim <- bin$dim
    L <- bdim[1] * bdim[2]

    ans <- .Fortran("herode",
                    cell  = as.integer(cell),
                    cnt   = as.integer(cnt),
                    n     = n,
                    bdim  = as.integer(bdim),
                    erode = integer(L),
                    ncnt  = integer(L),
                    ncell = integer(L),
                    sides = integer(L),
                    neib  = integer(6 * L),
                    exist = logical(L + 1),
                    PACKAGE = "hexbin") $ erode
    length(ans) <- n
    bin$erode <- ans
    class(bin) <- c(class(bin),"erodebin")
    bin
}

#hexagons.erode <- function(bin1, xbnds = bin1$xbnds, ybnds = bin1$ybnds,
#                           density, border = c(FALSE, FALSE),
#                           pen = c(2, 3), reshape = FALSE){
#  
#}


hexagons <-
function(dat, style = c("colorscale", "centroids", "lattice",
              "nested.lattice", "nested.centroids"),
         minarea = 0.05, maxarea = 0.8,
         mincnt = 1, maxcnt = max(dat$cnt), trans = NULL, colorcut,
         density = NULL, border = NULL, pen = NULL,
         colramp = function(n){ LinGray(n,beg = 90, end = 15) },
         verbose = getOption("verbose"))
{
    ## Warning:  presumes the plot has the right shape and scales
    ##           See plot.hexbin()
    ## Arguments:
    ##	dat= hexagon bin object
    ##  style = type of plotting
    ##          'centroids' =  symbol area is a function of the count,
    ##                  approximate location near cell center of
    ##                  mass without overplotting
    ##          'lattice' = symbol area is a function of the count,
    ##                  plot at lattice points
    ##          'colorscale'   = gray scale plot,
    ##                  color number determined by
    ##                  transformation and colorcut,
    ##                  area = full hexagons.
    ##          'nested.lattice'= plots two hexagons
    ##		   background hexagon
    ##			 area=full size
    ##			 color number by count in
    ##                        powers of 10 starting at pen 2
    ##                  foreground hexagon
    ##		   	 area by log10(cnt)-floor(log10(cnt))
    ##                        color number by count in
    ##                         powers of 10 starting at pen 12
    ##          'nested.centroids' = like nested.lattice but counts <10 are plotted
    ## 	minarea =  minimum symbol area as fraction of the binning cell
    ##  maxarea =  maximum symbol area as fraction of the binning cell
    ##  mincnt  =  minimum count accepted in plot
    ##  maxcnt  =  maximum count accepted in plot
    ##	trans   =  a transformation scaling counts into [0,1] to be applied
    ##		   to the counts for options 'centroids','lattice','colorscale':
    ##                   default=(cnt-mincnt)/(maxcnt-mincnt)
    ##  colorcut=  breaks for translating values between 0 and 1 into
    ##		   color classes.  Default= seq(0,1,17),
    ## 	density =  for hexagon graph paper
    ## 	border     plot the border of the hexagon, use TRUE for
    ##             hexagon graph paper
    ## Symbol size encoding:
    ##   Area= minarea + scaled.count*(maxarea-minarea)
    ##   When maxarea==1 and scaled.count==1, the hexagon cell
    ##   is completely filled.
    ##
    ##   If small hexagons are hard to see increase minarea.
    ## For gray scale encoding
    ##    Uses the counts scaled into [0,1]
    ##    Default gray cutpoints seq(0,1,17) yields 16 color classes
    ##    The color number for the first class starts at 2.
    ##         motif coding: black 15 white puts the first of the
    ##                       color class above the background black
    ##    The function subtracts 1.e-6 from the lower cutpoint to include
    ##    the boundary
    ## For nested scaling see the code
    ## Count scaling alternatives
    ##
    ##  log 10 and Poisson transformations
    ## 	trans <- function(cnt) log10(cnt)
    ##     min  inv   <- function(y) 10^y
    ##
    ##       trans <- function(cnt) sqrt(4*cnt+2)
    ##       inv   <- function(y) (y^2-2)/4
    ## Perceptual considerations.
    ##    Visual response to relative symbol area is not linear and varies from
    ##    person to person.  A fractional power transformation
    ##    to make the interpretation nearly linear for more people
    ##    might be considered.  With areas bounded between minarea
    ##    and 1 the situation is complicated.
    ##
    ##    The local background influences color interpretation.
    ##    Having defined color breaks to focus attention on
    ##    specific countours can help.
    ##
    ## Plotting the symbols near the center of mass is not only more accurate,
    ##    it helps to reduce the visual dominance of the lattice structure.  Of
    ##    course higher resolution binning reduces the possible distance between
    ##    the center of mass for a bin and the bin center.  When symbols
    ##    nearly fill their bin, the plot appears to vibrate.  This can be
    ##    partially controlled by reducing maxarea or by reducing
    ##    contrast.


    ##____________________Initial checks_______________________
    style <- match.arg(style) # so user can abbreviate
    #print(style)
    if(minarea <= 0)
        stop("hexagons cannot have a zero area, change minarea")
    if(maxarea > 1) warning("maxarea>1, hexagons may overplot")
    ##_______________ Collect computing constants______________
    cnt <- dat$cnt
    xbins <- dat$xbins
    shape <- dat$shape
    tmp <- hcell2xy(dat)
    good <- mincnt <= cnt & cnt <= maxcnt
    xnew <- tmp$x[good]
    ynew <- tmp$y[good]
    cnt <- cnt[good]
    sx <- xbins/diff(dat$xbnds)
    sy <- (xbins * shape)/diff(dat$ybnds)
    ##___________Transform Counts to Radius_____________________
    switch(style,
           centroids = ,
           lattice = ,
           colorscale = {
               if(is.null(trans)) {
                   rcnt <- {
                       if(maxcnt == mincnt) rep(1, length(cnt))
                       else (cnt - mincnt)/(maxcnt - mincnt)
                   }
               }
               else {
                   rcnt <- (trans(cnt) - trans(mincnt)) /
                       (trans(maxcnt) - trans(mincnt))
                   if(any(is.na(rcnt)))
                       stop("bad count transformation")
               }
               area <- minarea + rcnt * (maxarea - minarea)
               area <- pmin(area, maxarea)
               radius <- sqrt(area)
           },
           nested.lattice = ,
           nested.centroids = {
               diffarea <- maxarea - minarea
               step <- 10^floor(log10(cnt))
               f <- (cnt/step - 1)/9
               area <- minarea + f * diffarea
               area <- pmax(area, minarea)
               area <- pmin(area, maxarea)
               radius <- sqrt(area)
           }
           )
    ##______________Set Colors_____________________________
    switch(style,
           centroids = ,
           lattice = {if(is.null(pen))pen <- rep(1, length(cnt))
                      else if(length(pen)==length(cnt)) break
                      else if(length(pen)==1)pen <- rep(pen,length(cnt))
                      else stop("Pen is wrong length")
                      },
           nested.lattice = ,
           nested.centroids = {
               if(length(dim(pen))==2 & !is.null(pen)){
                 if( (dim(pen)[1] != length(cnt))
                    & ( dim(pen)[1] != ceiling(log10(max(cnt))) ) )
                   {
                   #print(dim(pen)[1])
                   #print(ceiling(log10(max(cnt))))
                   stop ("pen is not of right dimension")}
                 if( dim(pen)[1] == ceiling(log10(max(cnt))) ){
                   #tmp<-pen
                   #browser()
                   ind<-ceiling(log10(bin$cnt))
                   ind[ind==0]<-1
                   pen<-pen[ind,]
                   #print(pen)
                 }
                 else break
               }
               else{
                 pen<-matrix(0,length(cnt),2)
                 pen[,1] <- floor(log10(cnt)) + 2
                 pen[,2] <- pen[,1]+10
               }
           },
           colorscale = {
               ## MM: Following is quite different from bin2d's
               if(missing(colorcut))
                   colorcut <- seq(1, 0, length = 17)
               if(colorcut[1] > colorcut[length(colorcut)]){
                   colorcut[1] <- colorcut[1] + 1e-06
                   colorcut[length(colorcut)] <-
                       colorcut[length(colorcut)] - 1e-06
               } else {
                   colorcut[1] <- colorcut[1] - 1e-06
                   colorcut[length(colorcut)] <-
                       colorcut[length(colorcut)] + 1e-06

               }
               colgrp <- cut(rcnt, colorcut,labels = FALSE)
               ##NL: colramp must be a function accepting an integer n
               ##    and returning n colors
               clrs <- colramp(max(colgrp))
               pen <- clrs[colgrp]
           })
    ##__________________ Construct a hexagon___________________
    ## The inner and outer radius for hexagon in the scaled plot
    inner <- 0.5
    outer <- (2 * inner)/sqrt(3) # Now construct a point up hexagon symbol in data units
    dx <- inner/sx
    dy <- outer/(2 * sy)
    rad<-sqrt(dx^2+dy^2)
    polyx <- c(dx, dx, 0,  - dx,  - dx, 0, NA)
    polyy <- c(dy,  - dy, -2 * dy,  - dy, dy, 2 * dy, NA)
    ##_______________ Full Cell  Plotting_____________________
    switch(style,
           colorscale = {
               n <- length(xnew)
               seven <- rep(7, n)
               pltx <- rep(polyx, n) + rep(xnew, seven)
               plty <- rep(polyy, n) + rep(ynew, seven)
               polygon(pltx, plty, density = density,
                       border = if(!is.null(border)) border
                       else pen, col = pen)
             },
           nested.lattice = ,
           nested.centroids = {
               n <- length(xnew)
               seven <- rep(7, n)
               pltx <- rep(polyx, n) + rep(xnew, seven)
               plty <- rep(polyy, n) + rep(ynew, seven)
               polygon(pltx, plty, density = density,
                       border = if(!is.null(border)) border else pen[,1],
                       col = pen[,1])
           }
           )
    if(style == "colorscale") return("done `colorscale'")
    ##__________________ Symbol Center adjustments_______________
    if(style == "centroids" || style == "nested.centroids") {
        xcm <- dat$xcm[good]
        ycm <- dat$ycm[good]
        ## Store 12 angles around a circle and the replicate the first
        ## The actual length for these vectors is determined by using
        ## factor use below
        k <- sqrt(3)/2
        cosx <- c(1, k, 0.5, 0, -0.5,  - k, -1,  - k, -0.5, 0, 0.5, k, 1)/sx
        siny <- c(0, 0.5, k, 1, k, 0.5, 0, -0.5,  - k, -1,  - k, -0.5, 0)/sy
        ## Compute distances for differences after scaling into
        ## [0,size] x [0,aspect*size]
        ## Then there are size hexagons on the x axis
        dx <- sx * (xcm - xnew)
        dy <- sy * (ycm - ynew)
        dlen <- sqrt(dx^2 + dy^2)
        ## Find the closest approximating direction of the 12 vectors above
        cost <- ifelse(dlen > 0, dx/dlen, 0)
        tk <- (6 * acos(cost))/pi
        tk <- round(ifelse(dy < 0, 12 - tk, tk)) + 1
	## Select the available length for the approximating vector
        hrad <- ifelse(tk %% 2 == 1, inner, outer)
	## Rad is either an inner or outer approximating radius.
        ## If dlen + hrad*radius <= hrad, move the center dlen units.
        ## Else move as much of dlen as possible without overplotting.
        fr <- pmin(hrad * (1 - radius), dlen) # Compute the symbol centers
        ## fr is the distance for the plot [0,xbins]x[0,aspect*xbins]

        ## cosx and siny give the x and y components of this distance
        ## in data units
        xnew <- xnew + fr * cosx[tk]
        ynew <- ynew + fr * siny[tk]
    }
    ## ________________Sized Hexagon Plotting__________________
    ## scale the symbol by radius and add to the new center
    n <- length(radius)
    if(verbose)
        cat('length = ',length(pen),"\n",
            'pen = ', pen+1,"\n")
    #switch(style,
    #       centroids = ,
    #       lattice = {if(is.null(pen))pen <- rep(1, n)
    #                   else pen <- rep(pen, n)},
    #      nested.lattice = ,
    #       nested.centroids ={
    #         if(
    #            pen[,2] <- pen[,1] + 10
    #     }           )
    seven <- rep(7, n)
    pltx <- rep(polyx, n) * rep(radius, seven) + rep(xnew, seven)
    plty <- rep(polyy, n) * rep(radius, seven) + rep(ynew, seven)
    switch(style,
           centroids = ,
           lattice = polygon(pltx, plty, border = if(!is.null(border)) border else pen, col = pen),
           nested.lattice = ,
           nested.centroids ={
           polygon(pltx, plty, border = if(!is.null(border)) border else pen[,2], col = pen[,2])}
           )
    
} ## hexagons()


## Add a legend to a plot

hex.legend <-
function(legend, ysize, lcex, inner,
         style = c("colorscale", "centroids", "lattice",
         "nested.lattice", "nested.centroids"),
         minarea = 0.05, maxarea = 0.8, mincnt = 1, maxcnt, trans = NULL,
	 inv = NULL, colorcut, density = NULL, border = NULL, pen = NULL, 
         colramp = function(n){ LinGray(n,beg = 90,end = 15) })
{
    style <- match.arg(style) # check; user can abbreviate

    ## _______________tranformations_______________________
    switch(style,
           centroids = ,
           lattice = ,
           colorscale = {
               if(is.null(trans)) {
                   sc <- maxcnt - mincnt
                   bnds <- round(mincnt + sc * colorcut)
               }
               else {
                   con <- trans(mincnt)
                   sc <- trans(maxcnt) - con
                   bnds <- round(inv(con + sc * colorcut))
               }
           }
           )
    ## _______________Plotting______________________________
    switch(style,
           colorscale = {
               n <- length(bnds)
               spacing <- ysize/(n + 3)
               inner <- min(legend/3.5, (sqrt(3) * spacing)/2)
               dx <- inner/2
               dy <- dx/sqrt(3)
               midx <- legend/3
               textx <- (2 * legend)/3
               tx <- c(dx, dx, 0,  - dx,  - dx, 0, NA) + midx
               ty <- c(dy,  - dy, -2 * dy,  - dy, dy, 2 *
                       dy,NA)
               pen <- colramp(n)
               for(i in 1:(n)) {
                   polygon(tx, ty + i * spacing, 
                           col = pen[i], border = border,
                           density=density)# or pen[i], opt.? MM
                   text(textx, (i - 0.5) * spacing,
                        as.character(bnds[i]))
               }
               text(textx, (n - 0.5) * spacing, as.character(bnds[n]))
               text(legend/2, (n + 1.5) * spacing,
                    "Counts", cex = 1.7 * lcex)
           },
           centroids = ,
           lattice = {
               ## NL Solved hex overlap problem on legend
               ## Need to tackle too many categories
               dx <- inner/2
               dy <- dx/sqrt(3)
               radius <- sqrt(minarea + (maxarea - minarea) * colorcut)
               n <- length(radius)
               xmid <- legend/3
               tx <- rep(c(dx,  dx,     0, -dx, -dx, 0,    NA), n)
               ty <- rep(c(dy, -dy, -2*dy, -dy,  dy, 2*dy, NA), n)
               rad.units<-2*dy*radius
               shift<-c(rad.units[1],rad.units[1:(n-1)]+rad.units[2:(n)])
               labht<-max(strheight(as.character(bnds),cex=lcex))
               shift<-ifelse(labht>shift,labht,shift)
               seven <- rep(7, n)
               inc <- ysize/(n+3)
               if(inc > max(shift)) y <- inc * 1:n
               else{
                 y<-cumsum(shift)
                 extra.slop<- (n * inc) - y[n]
                 shift<-c(shift[1],shift[-1]+ (extra.slop/(n-1)))
                 y<-cumsum(shift)  #(y+(1/n)*extra.slop)-y[1]
                 ##delta<-max(log(shift))-min(log(shift))
                 ##fudge<- extra.slop*(diff(log(shift))/delta)                   
                 ##y<-c(y[1], y[-1]+ fudge )
               }
               textx <- rep((2 * legend)/3, n)
               ##  ____________________plotting______________________
               if(is.null(pen)) pen <- 1
               if(is.null(border)) border <- pen
               pltx <- tx * rep(radius, seven) + rep(xmid, 7 * n)
               plty <- ty * rep(radius, seven) + rep(y, seven)
               polygon(pltx, plty, col = pen, border = border, density=density)
               text(textx, y, as.character(bnds), cex = lcex)
               text(legend/2, (n + 2) * inc,
                    "Counts", cex = 1.7 * lcex)
           },
           nested.lattice = ,
           nested.centroids = {
               ## _____________x scaling_____________________________
               numb <- cut(floor(legend/inner), breaks = c(-1, 0, 2,4))
               ## Note: In old code
               ##	top breaks=c(-1,0,2,4,8), numb<- 5 and size=1:9
               if(is.na(numb))
                   numb <- 4
               switch(numb,
                      return("not enough space for legend"),
                      size <- 5,
                      size <- c(1, 5, 9),
                      size <- c(1, 3, 5, 7, 9))
               xmax <- length(size)
               diffarea <- maxarea - minarea
               radius <- sqrt(minarea + (diffarea * (size - 1))/9)
               txt <- as.character(size)
               ##___________________y scaling_____________________
               lab <- c("Ones", "Tens", "Hundreds",
                        "Thousands", "10 Thousands", "100 Thousands",
                        "Millions", "10 Millions",
                        "100 Millions", "Billions")
               power <- floor(log10(maxcnt)) + 1
               dx <- inner/2
               dy <- dx/sqrt(3)
               if(ysize/power < 16 * dy)
                   return("Not enough height for legend")
               xmid <- legend/2
               x <- inner * (1:xmax - (1 + xmax)/2) + xmid
               n <- length(x)
               tx <- rep(c(dx, dx,  0,   -dx,-dx,  0,   NA), n)
               ty <- rep(c(dy,-dy, -2*dy,-dy, dy, 2*dy, NA), n)
               seven <- rep(7, n)
               yinc <- 16 * dy
               y <- rep(3 * dy - yinc, xmax)
               delta1 <- 4.5 * dy
               delta2 <- 4.5 * dy
               ##  ____________________plotting______________________
               if(is.null(pen)){
                 pen<-matrix(1:power,power,2)
                 pen[,1]<-pen[,1]+1
                 pen[,2]<-pen[,2]+11
               }
               if(is.null(border)) border <- FALSE
               for(i in 1:power) {
                   y <- y + yinc
                   pltx <- tx + rep(x, seven)
                   plty <- ty + rep(y, seven)
                   polygon(pltx, plty, col = pen[i,1], border = border)
                   pltx <- tx * rep(radius, seven) + rep(x, seven)
                   plty <- ty * rep(radius, seven) + rep(y, seven)
                   polygon(pltx, plty, col = pen[i,2], border = border)
                   text(x, y - delta1, txt, adj = 0.5)
                   text(xmid, y[1] + delta2, lab[i], adj = 0.5)
               }
           }
           )## switch(style, *)
}

hmatplot <- function(namemat, rlabels, clabels,
                     brd = c(0.5, 0.7, 0.7, 0.5),
                     mai = rep(0.4, 4), unzoom = 1.04, cex = 1.5,
                     border = list(hbox=c(FALSE, FALSE),
                                   hdiff=rep(FALSE,6)),
                     pen = list( hbox = c(2, 3),hdiff = 4:9),
                     density = c(-1, -1, -1, 0, 0, 0),
                     size = 0.06, lwd = 2)
{
    ##frame() ## <<- MM: always?
    plot.new()
    ##___Get initial constants and check arguments_______________________
    nr <- nrow(namemat)
    nc <- ncol(namemat)
    if(missing(rlabels))
        rlabels <- rep("", nr)
    if(missing(clabels))
        clabels <- rep("", nc)
    if(any(mai > brd))
        stop("Border `brd' must be larger than margin `mai' on every side")
    ##_____________________________________________________________
    ##
    ##  Step 1 - Find x and y ranges that maximize the resolution
    ##           and maintain the shape
    tnam <- as.vector(namemat)
    bin1 <- get(tnam[1])
    tmp <- hcell2xy(bin1)
    rx <- range(tmp$x)
    ry <- range(tmp$y)
    for(i in 2:length(tnam)) {
        tmp <- hcell2xy(get(tnam[i]))
        rx <- range(rx, tmp$x)
        ry <- range(ry, tmp$y)
    }
    xbnds <- bin1$xbnds
    ybnds <- bin1$ybnds
    ratiox <- diff(rx)/diff(xbnds)
    ratioy <- diff(ry)/diff(ybnds)
    ratio <- max(ratioy, ratiox)

    rx <- mean(rx) + c(-1,1)*(unzoom * ratio * diff(xbnds))/2
    ry <- mean(ry) + c(-1,1)*(unzoom * ratio * diff(ybnds))/2
    print(rx)
    print(ry)
    ##_________________________________________________________
    ##  Step 2.  set up a matrix of plots
    oldpar <- par(no.readonly=TRUE)
    on.exit(par(oldpar))
    opar<-list()
    opar$global<- par(xpd = TRUE, xaxt = "n", yaxt = "n")
    din <- par("din")
    xsize <- din[1] - brd[2] - brd[4]
    ysize <- din[2] - brd[1] - brd[3]
    nrplot <- 2 * nr - 1
    ncplot <- 2 * nc - 1
    nshape <- (ysize/nrplot)/(xsize/ncplot)
    shape <- bin1$shape
    if(nshape < shape) {
        inc <- (xsize * (1 - nshape/shape))/2
        brd <- brd + c(0, inc, 0, inc)
        xsize <- xsize - 2 * inc
    }
    else {
        inc <- (ysize * (1 - shape/nshape))/2
        brd <- brd + c(inc, 0, inc, 0)
        ysize <- ysize - 2 * inc
    }
    pxsize <- xsize/ncplot
    pysize <- ysize/nrplot
    st <- c((brd[2] - mai[2])/din[1],
            (brd[2] + pxsize + mai[4])/din[1],
            (din[2] - brd[3] - pysize - mai[1])/ din[2],
            (din[2] - brd[3] + mai[3])/din[2])
    pxsize <- pxsize/din[1]
    pysize <- pysize/din[2]
    ##_________________________________________________________
    ## Step 3. produce each boxplot in the structure
    #browser()
    opar$boxpar<-list()
    for(i in seq(length=nr)) {
        for(j in seq(length=nc)) {
            px <- 2 * (j - 1) * pxsize
            py <- 2 * (i - 1) * pysize
            opar$boxpar <- c(opar$boxpar,
                             par(new=TRUE, fig = st + c(px, px, -py, -py),
                                 mai = mai))
            bin <- get(namemat[i, j])
            hboxplot(bin, rx, ry, border=border$hbox, pen = pen$hbox)
            if(i == 1)
                mtext(side = 3, clabels[j], line = 1, cex = cex)
            if(j == 1)
                mtext(side = 2, rlabels[i], line = 1, cex = cex)
        }
    }
    ##_____________________________________________________________
    ## Step 4. produce a difference plot for adjacent pairs in each row
    opar$diffpar.row<-list()
    if(nc > 1) {
        for(i in 1:nr) {
            py <- 2 * (i - 1) * pysize
            for(j in 1:(nc - 1)) {
                px <- (2 * j - 1) * pxsize
                opar$diffpar.row <- c(opar$diffpar.row,
                                      par(new=TRUE,
                                          fig = st + c(px, px, -py, -py)))
                bin1 <- get(namemat[i, j])
                bin2 <- get(namemat[i, j + 1])

                hdiffplot(bin1, bin2, rx, ry, pen = pen$hdiff,
                          border = border$hdiff,
                          size = size, lwd = lwd, xaxt = "n", yaxt = "n",
                          main = "")
            }
        }
    }
    ##_____________________________________________________________
    ## Step 5.  plot difference plot for adjacent pairs in each column
    opar$diffpar.col<-list()
    if(nr > 1) {
        for(j in 1:nc) {
            px <- 2 * (j - 1) * pxsize
            for(i in 1:(nr - 1)) {
                py <- (2 * i - 1) * pysize
                of <- par(new=TRUE, fig = st + c(px, px,  - py,  - py))
                if(!any("fig" == names(opar)))
                  opar$diffpar.col <- c(opar$diffpar.col, of)
                bin1 <- get(namemat[i, j])
                bin2 <- get(namemat[i + 1, j])
                hdiffplot(bin1, bin2, rx, ry, pen = pen$hdiff,
                          border = border$hdiff,
                          size = size, lwd = lwd, xaxt = "n", yaxt = "n",
                          main = "")
            }
        }
    }
    ##_______________________________________________________________
    ## Step 6.  cleanup
    invisible(list(xbnds = rx, ybnds = ry, xsize=xsize, ysize=ysize,
                   plot.par = opar))
    #invisible(list(xbnds = xbnds, ybnds = ybnds, plot.par = opar))
}## hmatplot()

plot.hexbin <-
    function(x, style = c("colorscale", "centroids", "lattice",
                "nested.lattice", "nested.centroids"),
             legend = 1, lcex = 1,
             minarea = 0.04, maxarea = 0.8, mincnt = 1, maxcnt = max(x$cnt),
             trans = NULL, inv = NULL,
             colorcut = seq(0, 1, length = min(17, maxcnt)),
             border = NULL, density = NULL, pen = NULL,
             colramp = function(n){ LinGray(n,beg = 90,end = 15) },
             xlab = "", ylab = "", verbose = getOption("verbose"), ...)
{
    ##NL: Added colramp argument to use fancy ramps
    if(!inherits(x,"hexbin")) stop("first argument must be a hexbin object")
    style <- match.arg(style) # check; user can abbreviate
    plot.new()
    ## MM: wrong in R (?):
    #frame()

    ##NL: Needed this to get the legend to work
    #split.screen(c(1,1))
    ##layout(matrix(c(2,1),1,2),c(5,1))
    ##NL: Layout will probably work better
    if(minarea < 0)
        stop("Minimum area must be non-negative")
    if(maxarea > 1)
        warning("Maximum area should be <= 1 this leads to overlapping hexagons")
    if(minarea > maxarea)
        stop("Minarea must be <- maxarea")
    if(length(colorcut) > 1) {
        if(colorcut[1] != 0)
            stop("Colorcut lower boundary must be 0")
        if(colorcut[length(colorcut)] != 1)
            stop("Colorcut upper boundary must be 1")
    }
    else {
        colorcut <- 1
    }

    shape <- x$shape
    oldpar <- par(no.readonly=TRUE)
    on.exit( {
        ##RG: pars are funny things
        ##oldpar$cin <- NULL; oldpar$din<-NULL;
        ##oldpar$cra<-NULL;oldpar$csi<-NULL;oldpar$cxy<-NULL;
        ##oldpar$gamma<-NULL;
        ##RG
        par(oldpar)
        #par(new = FALSE, mai = oldpar$mai, fig = oldpar$fig)
    } )

    din <- oldpar$din
    pin <- oldpar$pin
    mai <- oldpar$mai
    fig <- oldpar$fig
    xsize <- pin[1]
    ysize <- pin[2]
    if(is.logical(legend)) {
        if(legend)
            stop("Give the legend width")
        else legend <- 0
    }
    xsize <- xsize - legend
    if(xsize < 1)
        stop("plot width too small")
    if(shape * xsize <= ysize) {
        center <- (ysize - shape * xsize)/2
        mai[1] <- mai[1] + center
        mai[3] <- mai[3] + center
        mai[4] <- mai[4] + legend
        ysize <- shape * xsize
    }
    else {
        center <- (xsize - ysize/shape)/2
        mai[2] <- mai[2] + center
        mai[4] <- mai[4] + center + legend
        xsize <- ysize/shape
    }
    if(legend > 0) {
        if(!is.null(trans) && is.null(inv))
            stop("Must supply the inverse transformation")

        if(verbose)
            cat("plot.hexbin( legend > 0):  ... hex.legend()\n")

        #screen(1,new = FALSE)## (MM: not in bin2d)
        mai[2] <- mai[2] + xsize
        mai[4] <- mai[4] - legend
        par(fig = fig, mai = mai, new=TRUE)
        plot.new()
        plot.window(c(0, legend), c(0, ysize))
             ##type = "n", axes = FALSE, xlab = "", ylab = "", main = "")
        inner <- xsize/x$xbins
        outer <- (inner * sqrt(3))/2
        ##switch(style,
        ##       lattice = ,
        ##       centroids = {
        ##           if(length(colorcut) * outer > ysize - 1) {
        ##               warning("Colorcut is being shortened")
        ##               colorcut <- seq(0, 1,
        ##                               max(1, floor((ysize - 1)/outer)))
        ##           }
        ##       }
        ##       )
        hex.legend(legend, ysize, lcex = lcex, inner,
                style = style, minarea = minarea, maxarea = maxarea,
                mincnt = mincnt, maxcnt = maxcnt, trans, inv,
                colorcut, density = density, border = border, pen = pen,
                colramp = colramp)
    if(verbose) cat("plot.hexbin(): calling screen(1,...); plot(); hexagons()\n")
        mai[2] <- mai[2] - xsize
        mai[4] <- mai[4] + legend
        ## MM: bin2d does   par(new = TRUE)
    }
    if(verbose) cat("plot.hexbin(): calling screen(1,...); plot(); hexagons()\n")
    #screen(1,new = FALSE)## (MM: not in bin2d)
    if(legend>0){
      par(new=TRUE, mai = mai, fig = fig)
      plot.new()
    }
    
    plot(x$xbnds, x$ybnds, type = "n", xlab = xlab, ylab = ylab, ...)
    hexagons(x, style = style,
             minarea = minarea, maxarea = maxarea,
             mincnt = mincnt, maxcnt = maxcnt,
             trans = trans, colorcut, density = density,
             border = border, pen = pen, colramp = colramp, verbose = verbose)
    
    invisible(par(no.readonly=TRUE))
} ## plot.hexbin()

smooth.hexbin <- function(bin, wts = c(48, 4, 1))
{
    if(!inherits(bin,"hexbin")) stop("first argument must be a hexbin object")
    cell <- bin$cell - 1
    n <- length(cell)
    cnt <- bin$cnt
    xbins <- bin$xbins
    bdim <- bin$dim
    row <- bdim[1]
    col <- bdim[2]
    ncol <- col + 4
    nrow <- row + 4
    nmax <- ncol * nrow
    sm <- rep(0, nmax)
    nr <- cell %/% col + 2
    nc <- cell %% col + 3
    pad <- rep(0, nmax - n)
    cell <- c(nr * ncol + nc, pad)
    cnt <- c(cnt, pad)
    ans <- .Fortran("hsm",
                    cell = as.integer(cell),
                    cnt  = as.integer(cnt),
                    n    = n,
                    nmax = as.integer(nmax),
                    sm   = as.integer(sm),
                    ncol = as.integer(ncol),
                    wts  = as.integer(wts),
                    PACKAGE = "hexbin")[c("cell","cnt","n")]
    n <- ans$n
    length(ans$cell) <- length(ans$cnt) <- n
    ans$xbins <- xbins + 4
    ans$shape <- bin$shape
    ans$xbnds <- bin$xbnds + c(-2, 2)* diff(bin$xbnds)/xbins
    ans$ybnds <- bin$ybnds + c(-2, 2)* diff(bin$ybnds)/xbins
    ans$dim <- c(nrow, ncol)
    ans$wts <- wts
    class(ans) <- c(class(bin),"sm.hexbin")
    ans
}


identify.hexbin <- function(x, labels = x$cnt, offset = 0, ...)
{
    if(length(labels) != x$n)
        stop("labels not the same length as number of cells")
    ##NL: Should this be a warning?

    ## -> typically default method:
    identify(hcell2xy(x), labels = labels, offset = offset, ...)
}

summary.hexbin <- function(object, ...) {
  cat(paste("Call:", deparse(object$call), "\n"))
  cat("Hexagon Grid Dimensions: ", object$dim[1], "by", object$dim[2],"\n")
  summary(data.frame(cell = object$cell, count = object$cnt,
                     xcenter = object$xcm, ycenter = object$ycm),
          ...)
  ##-> NextMethod()
}

hray <-
function(x, y, val, lowbnd = NULL, highbnd = NULL,
         minval = if(is.null(lowbnd)) min(val) else min(lowbnd),
         maxval = if(is.null(highbnd)) max(val) else max(highbnd),
         raylength = 0.3, raylwd = 2, raypen = 1, dotlength = 0.06,
         dotsides = 8, dotlwd = 1, dotden = 0, dotborder = TRUE,
         dotpen = 1,
         ticlength = 0.06, ticsides = 8, ticlwd = 1, ticpen = 1,
         arclength = 0.75, arcsides = 18, arclwd = 1, arcden = NULL,
         arcborder = FALSE, arcpen = 2,
         minangle =  - pi/2, maxangle = pi/2, clockwise = FALSE)
{
    n <- length(x)
    ## Puin <- par("uin") :
    P <- par(c("pin", "usr"))
    Puin <- c(P$pin[1]/(P$usr[2]-P$usr[1]),
              P$pin[2]/(P$usr[4]-P$usr[3]))
    if(clockwise && maxangle > minangle)
        maxangle <- maxangle - 2 * pi
    angle.con <- (maxangle - minangle)/(maxval - minval)

    ## raylength, dotlength, ticlength == FALSE  ---> treated as 0 automatically

    if(length(raylength) == 1)
        raylength <- rep(raylength, length(val))
    ## __________________plot confidence arcs_________________________
    if(!is.null(lowbnd)) {
        if(is.null(highbnd))
            stop("Must also have upper confidence bound `highbnd'")
        if(length(arclength) == 1)
            arclength <- arclength * raylength
        arc.xunits <- arclength/Puin[1]
        arc.yunits <- arclength/Puin[2]
        arc <- seq(minangle, maxangle, length = arcsides)
        arc.cos <- cos(arc)
        arc.sin <- sin(arc)
        low.angle <- minangle + angle.con * (lowbnd - minval)
        low.angle.cos <- cos(low.angle)
        low.angle.sin <- sin(low.angle)
        high.angle <- minangle + angle.con * (highbnd - minval)
        high.angle.cos <- cos(high.angle)
        high.angle.sin <- sin(high.angle)
        for(i in 1:length(val)) {
            nx <- x[i]
            ny <- y[i]
            low <- low.angle[i]
            high <- high.angle[i]
            good <-
                if(clockwise)
                    high < arc & arc < low
                else low < arc & arc < high
            if(!any(good)) {
                nx <- c(nx, nx + arc.xunits[i] *
                        c(low.angle.cos[i], high.angle.cos[i]))
                ny <- c(ny, ny + arc.yunits[i] *
                        c(low.angle.sin[i], high.angle.sin[i]))
            }
            else {
                nx <- c(nx, nx + arc.xunits[i] *
                        c(low.angle.cos[i], arc.cos[good],
                          high.angle.cos[i]))
                ny <- c(ny, ny + arc.yunits[i] *
                        c(low.angle.sin[i], arc.sin[good],
                          high.angle.sin[i]))
            }
            polygon(nx, ny, border = arcborder, density = arcden,
                    col = arcpen, lwd = arclwd)

        }
    }
    ##____________________ plot tics_____________________________
    if(ticlength > 0) {
        tic.angle <- matrix((2 * pi * (1:ticsides - 1))/ticsides, ncol = 2)
        tic.angle <- as.vector(t(cbind(tic.angle,
                                       rep(NA, nrow(tic.angle)))))
        tic.n <- length(tic.angle)
        cray <- (ticlength * cos(tic.angle))/Puin[1]
        sray <- (ticlength * sin(tic.angle))/Puin[2]
        polyx <- rep(x, rep(tic.n, n)) + rep(cray, n)
        polyy <- rep(y, rep(tic.n, n)) + rep(sray, n)
        lines(polyx, polyy, lwd = ticlwd, col = ticpen)
    }
    ##___________________plot dots___________________________________
    if(dotlength > 0) {
        dot.angle <- (2 * pi * (1:dotsides - 1))/dotsides
        dot.cos <- c((dotlength * cos(dot.angle))/Puin[1], NA)
        dot.sin <- c((dotlength * sin(dot.angle))/Puin[2], NA)
        dot.n <- length(dot.cos)
        ##RG:dot.n, n)) + rep(dot.sin, n),
        polygon(rep(x, rep(dot.n, n)) + rep(dot.cos, n),
                rep(y, rep(dot.n, n)) + rep(dot.sin, n),
                border = dotborder, lwd = dotlwd, density = dotden)
    }
    ##_____________________plot rays______________________________
    if(raylength[1] > 0) {
        angle <- minangle + angle.con * (val - minval)
        ray.x <- raylength/Puin[1]
        ray.y <- raylength/Puin[2]
        segments(x, y,
                 x + ray.x * cos(angle),
                 y + ray.y * sin(angle), lwd = raylwd, col = raypen)
    }
}## hray()

####--- This should really be somewhat compatible to  base::col2rgb()
####--> Use 3-row (and not 3-column) matrices !

rgb2hsv <- function(r, g = NULL, b = NULL, delta = 1)
{
    rgb <-
        if(is.null(g) && is.null(b)) as.matrix(r)
        else rbind(r,g,b)
    if(!is.numeric(rgb)) stop("rgb matrix must be numeric")
    d <- dim(rgb)
    if(d[1] != 3) stop("rgb matrix must have 3 rows")
    if(delta != 1)# revert delta corrected hsv values
        rgb <- rgb ^ (1/delta)
    n <- d[2]
    if(n == 0)
        return(cbind(c(h=1,s=1,v=1))[,0])
    ## else:
    if(any(rgb > 1) || any(rgb < 0)) stop("rgb values must be in [0,1]")

    storage.mode(rgb) <- "double"
    hsv <- .C("RGBtoHSV",
              rgb,
              hsv = double(3 * n),
              n,
              PACKAGE = "hexbin")$hsv
    dim(hsv) <- c(3,n)
    rownames(hsv) <- c("h","s","v")
    hsv
}
#Lib load function
.First.lib <- function(lib, pkg) {
  #require(methods)
  library.dynam("hexbin", pkg, lib)
  #where <- match(paste("package:", pkgname, sep=""), search())
  #initClasses(where)
  #cacheMetaData(as.environment(where))
}
