Author Topic: Need help understanding the help  (Read 12209 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Need help understanding the help
« on: October 12, 2010, 06:03:42 PM »
So here is the section I'm referring to
Quote from: Online Dxf help
420-427

32-bit integer value. When used with True Color; a 32-bit integer representing a 24-bit color
value. The high-order byte (8 bits) is 0, the low-order byte an unsigned char holding the Blue
value (0-255), then the Green value, and the next-to-high order byte is the Red Value. Convering
this integer value to hexadecimal yields the following bit mask: 0x00RRGGBB. For example, a
true color with Red==200, Green==100 and Blue==50 is 0x00C86432, and in DXF, in decimal,
13132850

Found here [ http://images.autodesk.com/adsk/files/acad_dxf0.pdf ]

What I'm trying to see is what is the viewport override color for a layer.  I can find what I'm looking for ( at least I think it's what I'm looking for ) within the extension dictionary of a layer.  This code is just for layer 0, but you can see what I'm getting at.

Code: [Select]
(defun c:Test ( / Data )
   
    (setq Data (entget (tblobjname "layer" "0")))
    ;(setq Data (entget (cdr (assoc 360 Data))))
    (setq Data (dictsearch (cdr (assoc 360 Data)) "ADSK_XREC_LAYER_COLOR_OVR"))
    (while (setq Data (member '(102 . "{ADSK_LYR_COLOR_OVERRIDE") Data))
        (prompt (strcat "\n Viewport handle: " (cdr (assoc 5 (entget (cdr (assoc 335 Data))))) "  Color code: " (rtos (cdr (assoc 420 Data)) 2 0)))
        (if (equal (car (cadddr Data)) 430)
            (princ (strcat "  Color book: " (cdr (cadddr Data))))
        )
        (setq Data (cdr Data))
    )
    (princ)
)

This is what prints out when I have it set to the true color value that is listed in the help, r = 200 g =100 b = 50

 Viewport handle: 112  Color code: -1027054542

As you can see it doesn't match what the help file says.  So I'm wondering how to get from one to the other?  It seems like all Acad colors start with ' -10234 ', with color 1 ending with ' 10175 ' and color 255 ending with ' 09921 '.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Need help understanding the help
« Reply #1 on: October 12, 2010, 06:31:01 PM »
That's pretty weird, now I'm curious too...

FYI, same result using VL:

Code: [Select]
(defun c:TestVL ( / xRec Typ Val )
  (vl-load-com)

  (if
    (not
      (vl-catch-all-error-p
        (setq xRec
          (vl-catch-all-apply 'vla-item
            (list
              (vla-getExtensionDictionary
                (vla-item
                  (vla-get-layers
                    (vla-get-ActiveDocument
                      (vlax-get-acad-object)
                    )
                  )
                  "0"
                )
              )
              "ADSK_XREC_LAYER_COLOR_OVR"
            )
          )
        )
      )
    )
    (progn
      (vla-getXrecordData xRec 'typ 'val)

      (print
        (apply 'mapcar
          (cons 'cons
            (mapcar 'VariantValue (list typ val))
          )
        )
      )
    )
  )
  (princ)
)

(defun VariantValue ( value )
  (cond
    ( (eq 'variant (type value))

      (VariantValue (vlax-variant-value value))
    )
    ( (eq 'safearray (type value))

      (mapcar 'VariantValue (vlax-safearray->list value))
    )
    ( value )
  )
)


JohnK

  • Administrator
  • Seagull
  • Posts: 10595
Re: Need help understanding the help
« Reply #2 on: October 12, 2010, 09:26:22 PM »
* Se7en racks brain...

I think i remember reading something about this a few years back when the true color thing started showing up. I remember the author was stumped about the formula or something but found something that worked. I'll do some searching and get back to ya.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10595
Re: Need help understanding the help
« Reply #3 on: October 12, 2010, 09:37:55 PM »
Found this right away, which looks promising, but i haven't looked further then to see it had SOME conversion functions.

http://www.jtbworld.com/lisp/DisplayColorProperties.htm
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Need help understanding the help
« Reply #4 on: October 12, 2010, 10:00:59 PM »
No knowledge of true color but did find this in my collection of stuff.
Quote
Adding the ability to select True Colors to your LISP programs

If you take a look at the help files for both AutoCAD 2004 and 2005 you find the
old function ACAD_COLORDLG listed for AutoLISP. However, the ACAD_COLORDLG
function doesn't support True Colors in AutoCAD though. So was it over looked,
is it missing... really it is just undocumented. The function you need in your
code if you want to all the user to select True Colors is ACAD_TRUECOLORDLG.
Since the function is undocumented it took a little while to exactly figure out
the syntax for this function a year ago. Below is what I know of the function
itself and its companion command to select True Colors at the command line.

Dialog Box
Syntax: (acad_truecolordlg color [allowbylayer])

Example:
(acad_truecolordlg 137 nil)
((62 . 137))



Alternative to use True color:
(acad_truecolordlg (cons 420 2594))

Alternative to use Color Book color:
(acad_truecolordlg (cons 430 "PANTONE(R) a & i-cotton$PANTONE 11-4800TC"))


Command Line
Syntax: (acad_truecolorcli color [allowbylayer] [alternatePrompt])

Example:
(acad_truecolorcli 137)
New Color [Truecolor/COlorbook] <137>:
((62 . 137))

Other return values:
Returns ACI as: ((62 . 144))

Returns True Color as: ((62 . 137) (420 . 2594437))

Returns Color Book Color as:
((62 . 254) (420 . 15789027) (430 . "PANTONE(R) a & i-cotton$PANTONE 11-4800 TC"))

Sincerely,
  Lee Ambrosius
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #5 on: October 13, 2010, 12:02:06 PM »
Thanks for trying guys, but both are a no go.  They both get the correct RGB color value, as the help states, but doesn't get the number that is associated with the vp overrides.  There must be another calculation that is missing.  I'll see what I can find out.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #6 on: October 13, 2010, 12:25:03 PM »
Query sent to ADN.  Lets see if they can help this time.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE3

  • Guest
Re: Need help understanding the help
« Reply #7 on: October 14, 2010, 02:12:58 PM »
420-427

32-bit integer value. When used with True Color; a 32-bit integer representing a 24-bit color
value. The high-order byte (8 bits) is 0, the low-order byte an unsigned char holding the Blue
value (0-255), then the Green value, and the next-to-high order byte is the Red Value. Convering
this integer value to hexadecimal yields the following bit mask: 0x00RRGGBB. For example, a
true color with Red==200, Green==100 and Blue==50 is 0x00C86432, and in DXF, in decimal,
13132850

Did a quick test on the decimal number above and it returns (if I try to pass a negative value it simple crashes AutoCAD) do not know if helps:
Quote
unsigned long rgb = 13132850;
acutPrintf ( _T("\nR=%d G=%d B=%d"), GetRValue(rgb), GetGValue(rgb), GetBValue(rgb) );



Command: test
R=50 G=100 B=200

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Need help understanding the help
« Reply #8 on: October 14, 2010, 03:44:34 PM »
Here is another tid bit.
Wish I had kept better notes on where it came from.
Code: [Select]
(defun ContrastingColor(TrueColorNumber /  colorpoint cubecorners distances
maxdistance cpt)
  ;; TrueColor-split - takes a TrueColor number and returns a list of integers the form '(red green blue)
  ;;
  (defun TrueColor-split ( c / )
    (list (lsh (fix c) -16)
   (lsh (lsh (fix c) 16) -24)
   (lsh (lsh (fix c) 24) -24)
   )
    )
  ;; TrueColor-make - takes TrueColor components (red green blue) and
creates a single TrueColor integer
  ;;
  (defun TrueColor-make ( r g b /  )
    (+ (lsh (fix r) 16)
       (lsh (fix g) 8)
       (fix b)
       )
    )
  ;;Get rgb for supplied truecolor
  (setq
    ColorPoint (TrueColor-Split trueColorNumber)
  ;;Get distance to each of the 6 corners of the colorcube
    CubeCorners '(
    (0 0 0)
    (0 0 255)
    (0 255 0)
    (0 255 255)
    (255 0 0)
    (255 0 255)
    (255 255 255)
    )
  ;;Get the greatest distance
    distances (mapcar '(lambda (cpt)
    (distance colorpoint cpt)
    )
        cubecorners
        )
    MaxDistance (apply 'max distances)
    cpt (nth (- 7 (length (member MaxDistance distances))) CubeCorners)
    )     ;end setq


  ;;Convert this corner back to a truecolor and return it
  (apply 'TrueColor-make cpt)
  )     ;end contrastingcolor
« Last Edit: October 14, 2010, 05:49:28 PM by CAB »
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #9 on: October 14, 2010, 03:57:34 PM »
Thanks guys.  I'll see what I can do with the information provided.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Need help understanding the help
« Reply #10 on: October 15, 2010, 12:51:39 AM »
************************************Edited ********************************
 The Next Post I Think Has The Answer I Produced The Help File Value By Using The True Color Tab And Entering A Rgb Value The Other Value Is From Entering A Index Number
************************************Edited ********************************

I might have found a pattern
Sorry about the mess

The only layer in the drawing was layer "0"

There were 16 viewports in drawing
The color override was color 1 to 10 and 120 to 125

It did not go in order but the values after Command: GETLAYERVPOVERRIDE
Were generated using the GetViewportOverrides method

The values after Command: GETLAYERVPOVERRIDEEXTDIC
Were from getting the layer ExtensionDictionary and printing the DxfCode and value from the Xrecord "ADSK_XREC_LAYER_COLOR_OVR"

So as you can see
1 = -1023410175
2 = -1023410174
3 = -1023410173
......
......
125 = -1023410051

Code: [Select]
Command: GETLAYERVPOVERRIDE
red
yellow
green
cyan
blue
10
9
8
white
magenta
124
123
122
121
120
125
Command: GETLAYERVPOVERRIDEEXTDIC
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680016))
(420,-1023410175)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680040))
(420,-1023410174)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680072))
(420,-1023410173)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680104))
(420,-1023410172)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680136))
(420,-1023410171)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680168))
(420,-1023410166)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680200))
(420,-1023410167)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680232))
(420,-1023410168)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680264))
(420,-1023410169)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680296))
(420,-1023410170)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680392))
(420,-1023410052)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680424))
(420,-1023410053)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680456))
(420,-1023410054)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680488))
(420,-1023410055)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680520))
(420,-1023410056)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680616))
(420,-1023410051)
(102,})

Here is the C# code if anyone wants to see if they get the same values

Code: [Select]
[CommandMethod("GetLayerVpOverRide")]
        public void GetLayerVpOverRide()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                LayerTable lyrTbl = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
                LayerTableRecord lyrTblRec = (LayerTableRecord)lyrTbl["0"].GetObject(OpenMode.ForRead);
                ObjectIdCollection vpObjIds = new ObjectIdCollection();
                vpObjIds = db.GetViewports(false);
                foreach (ObjectId vpId in vpObjIds)
                {
                    LayerViewportProperties lyrVp = lyrTblRec.GetViewportOverrides(vpId);
                    ed.WriteMessage("\n" + lyrVp.Color);
                }
                trx.Commit();
            }   
        }
        [CommandMethod("GetLayerVpOverRideExtDic")]
        public void GetLayerVpOverRideExtDic()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                LayerTable lyrTbl = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
                LayerTableRecord lyrTblRec = (LayerTableRecord)lyrTbl["0"].GetObject(OpenMode.ForRead);
                DBDictionary extDic = (DBDictionary)trx.GetObject(lyrTblRec.ExtensionDictionary, OpenMode.ForRead, false);
                Xrecord xRec = (Xrecord)trx.GetObject(extDic.GetAt("ADSK_XREC_LAYER_COLOR_OVR"), OpenMode.ForRead, false);
                ResultBuffer reBuff = xRec.Data;
                TypedValue[] typVal = reBuff.AsArray();
                for (int i = 0; i < typVal.Length; i++ )
                {
                    ed.WriteMessage("\n" + typVal[i].ToString());
                }
                trx.Commit();
            }
        }

« Last Edit: October 15, 2010, 01:37:18 AM by Jeff H »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Need help understanding the help
« Reply #11 on: October 15, 2010, 01:23:23 AM »
Now I think I got it

If you look at the first one the Number is what you expect from the help file.
The color can be saved by RGB or index

So when I selected the color from the Index tab I got the unexpected value
If I went to the true color tab and entered the RGB value then the RGB value was used and got the number the help file said you would.

Code: [Select]
Command: GETLAYERVPOVERRIDE
200,100,50
255,255,0
green
cyan
blue
10
9
8
white
magenta
124
123
122
121
120
125
Command: GETLAYERVPOVERRIDEEXTDIC
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680016))
(420,-1027054542)
(102,})
(102,{ADSK_LYR_COLOR_OVERRIDE)
(335,(2129680040))
(420,-1023410432)
(102,})

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #12 on: October 15, 2010, 11:41:00 AM »
I think the case is that you can't get the true value of the color through Lisp.  There is some type of formatting, so that the numbers do not match.  It seems the help I quoted refers to the color of an entity, which it does, and the numbers come out just like the help says, but when dealing with an override, then an extra formatting takes place, and Lisp doesn't see the real value.  One could get the real values knowing how it works, but one can't set the value with lisp.  If you set the value with lisp, then it will act like no override is there, at least in my test that is what happened.

I looked at the layer with dbview, and it doesn't even show the color in the dxf code of the xrecord of the override.  I was thinking that maybe something else got added to the drawing when one assigns an override, but I didn't see anything in the dbview that shows something new is added.  I thought I saw somewhere a program that would watch the database as you work, and you can see what gets added in real time, but I wasn't able to find a program like that, so if someone knows what I'm talking about, I would like to try that next, and see if something else is getting added when assigning an override to a layer.


Jeff,

  ADN even said that the only way to get the real value is with .Net of Arx, so I guess if one wanted to get the value, then that is the way to go.  I just wanted to see if it could be done though lisp.  Thanks for providing a solution so that others who might need/want something like this, have it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Need help understanding the help
« Reply #13 on: October 15, 2010, 12:57:11 PM »
I'm not sure this is going to help, but,  here it is anyway.  It may shed some light
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Need help understanding the help
« Reply #14 on: October 15, 2010, 02:04:51 PM »
In .Net using the LayerViewportProperties.Color Property which is the override you can not get the truecolor you can get the ACI or RGB

You only get that value if the override is enterd as a RGB

The ones circled in red give you the true color you looking for in Lisp, .NET

The ones circled in blue give you the other value for Lisp, .NET

If the override is red or number 1 you get the same value,  so for R = 255, G = 0, B = 0 you would get the same answer




Code: [Select]
(420,-1023475712)    ACI# 1
(420,-1023410432)    ACI# 2
(420,-1040122112)    ACI# 3
(420,-1040121857)    ACI# 4
(420,-1040187137)    ACI# 5
(420,-1023475457)    ACI# 6
(420,-1023410177)    ACI# 7
(420,-1031765888)    ACI# 8
(420,-1027555136)    ACI# 9


(420,-1023410175)    ACI# 1
(420,-1023410174)    ACI# 2
(420,-1023410173)    ACI# 3
(420,-1023410172)    ACI# 4
(420,-1023410171)    ACI# 5
(420,-1023410170)    ACI# 6
(420,-1023410169)    ACI# 7
(420,-1023410168)    ACI# 8
(420,-1023410167)    ACI# 9


Code: [Select]
[CommandMethod("GetLayerVpOverRide")]
        public void GetLayerVpOverRide()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                LayerTable lyrTbl = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
                LayerTableRecord lyrTblRec = (LayerTableRecord)lyrTbl["0"].GetObject(OpenMode.ForRead);
                ObjectIdCollection vpObjIds = new ObjectIdCollection();
                vpObjIds = db.GetViewports(false);
                foreach (ObjectId vpId in vpObjIds)
                {
                    LayerViewportProperties lyrVp = lyrTblRec.GetViewportOverrides(vpId);
                    ed.WriteMessage("\n" + lyrVp.Color + "        " + "ACI# " + lyrVp.Color.ColorIndex);
                }
               
                trx.Commit();
            }   
        }
        [CommandMethod("GetLayerVpOverRideExtDic")]
        public void GetLayerVpOverRideExt()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {               
                LayerTable lyrTbl = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
                LayerTableRecord lyrTblRec = (LayerTableRecord)lyrTbl["0"].GetObject(OpenMode.ForRead);
                DBDictionary extDic = (DBDictionary)trx.GetObject(lyrTblRec.ExtensionDictionary, OpenMode.ForRead, false);
                Xrecord xRec = (Xrecord)trx.GetObject(extDic.GetAt("ADSK_XREC_LAYER_COLOR_OVR"), OpenMode.ForRead, false);
                ResultBuffer reBuff = xRec.Data;
                TypedValue[] typVal = reBuff.AsArray();
                int j = 1;
                for (int i = 2; i < typVal.Length; i = i + 4 )
                {
                    ed.WriteMessage("\n" + typVal[i].ToString() + "    ACI# " + j);
                    if (j == 9)
                    {                       
                        j = 1;
                    }
                    else
                    {
                        j++;
                    }
                }
                trx.Commit();
            }
        }



T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #15 on: October 15, 2010, 02:57:57 PM »
Jeff,

Here are some of the base information that I've seen.

    ; -1040187392 = 0,0,0
    ; -1040187391 = 0,0,1
    ; -1040187136 = 0,1,0
    ; -1040121856 = 1,0,0
    ; -1023410177 = 255,255,255
    ; -1023410175 = 1 ( autocad standard color )

I haven't looked into the color books too much, as that seems hidden.


Tim,

Some of the codes in there are good, but they don't show how to get the color from the overrides though, as nothing does.  Thanks for sharing it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #16 on: October 15, 2010, 04:36:41 PM »
Well the ADN was really helpful, and maybe the information might be helpful to others.  Here is the code they provided ' trueColorToRGB ', with comments from the email about what is happening.

Code: [Select]
(defun trueColorToRGB (tcol)
    ;|
        From: Wayne Brill ( ADN )
       
        My Colleague Adam Nagy has this suggestion:
        >> >> >>
        In that case the top byte is not 0 as the DevNote says, but it’s 1100 0010...
        just like when using ActiveX API to get the color value for Hatch gradients.
        Also, in that case when returning the r value the top byte needs to be cleared
        just like in case of getting the other values..
        So the
        (setq r (lsh tcol -16))
        In the trueColorToRGB function should be changed to
        (setq r (lsh (lsh tcol 8) -24))
        Not sure what that top byte means exactly, but it’s the exact value that you get in case of the hatch gradient through ActiveX
        << << <<
    |;

    ;(setq r (lsh tcol -16))
    (setq r (lsh (lsh tcol 8) -24))
    (setq g (lsh (lsh tcol 16) -24))
    (setq b (lsh (lsh tcol 24) -24))
    ;(princ (list r g b))
)
(defun c:Test ( / Data )
   
    (setq Data (entget (tblobjname "layer" "0")))
    ;(setq Data (entget (cdr (assoc 360 Data))))
    (setq Data (dictsearch (cdr (assoc 360 Data)) "ADSK_XREC_LAYER_COLOR_OVR"))
    (while (setq Data (member '(102 . "{ADSK_LYR_COLOR_OVERRIDE") Data))
        (prompt
            (strcat
                "\n Viewport handle: "
                (cdr (assoc 5 (entget (cdr (assoc 335 Data)))))
                "  Color code: " (rtos (cdr (assoc 420 Data)) 2 0)
                " "
                (vl-princ-to-string (trueColorToRGB (cdr (assoc 420 Data))))
            )
        )
        (if (equal (car (cadddr Data)) 430)
            (princ (strcat "  Color book: " (cdr (cadddr Data))))
        )
        (setq Data (cdr Data))
    )
    (princ)
)

It now prints the correct value for RGB colors.  I'll have to change it a little to accommodate standard colors.  Now I have to study ' byte ' and ' bit ' a little more, so that I can understand what is happening.

Example:
 Viewport handle: 112  Color code: -1036733749 (52 178 203)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Need help understanding the help
« Reply #17 on: October 24, 2010, 03:24:15 PM »
Hi Tim,

I found this thread really interesting and spent some time attempting to understand the arrangement of the 24-bit RGB integer, and how the various values are calculated from it.

Here is my take on it:

The RGB value is a 24-bit integer (i.e. composed of 3-bytes, the first being blue, then green, and the highest being red). To understand it, I created this diagram (using the RGB value from your first example).

When calculating the values, here was the previous function, as supplied by Tim's post:

Code: [Select]
(defun TrueColor-split ( c / )
  (list (lsh (fix c) -16)
        (lsh (lsh (fix c) 16) -24)
        (lsh (lsh (fix c) 24) -24)
  )
)

Hence to obtain, say, the Green value we use:

Code: [Select]
(lsh (lsh (fix c) 16) -24)
Here, all the bit-values are shifted 16 bits to the left, and being in a 32-bit environment the Red bits are consequently discarded and the Green bits occupy the 4th byte (located above the previous red byte), with the Blue bits occupying the 3rd byte (previously Red).

All the values are then shifted 24 bits to the right, and hence the green bits now occupy the first byte (previously blue), and the blue values are discarded, leaving just the green value which is returned.

Notice that, when calculating the Red value, the bits are only shifted down by 16 bits (discarding the green/blue bits) - this method hence assumes the 4th byte is empty and so doesn't bother to discard any possible values in the 4th byte.

However, in your override example, the 4th byte has value, and hence the assumption is not valid.

The correction, as supplied in your last post corrects this by first shifting the Red bits left by 8-bits, discarding any values that may be in the 4th byte, before shifting all the bits back down by 24 bits (3 bytes), meaning the Red values are now sitting in the 1st byte.

I hope my reasoning has been somewhat clear, and please don't take this that I'm teaching you to suck eggs, this was more for my own understanding that I thought I'd share. And, of course, if you see something wrong with the above, please do let me know.

Lee

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Need help understanding the help
« Reply #18 on: October 24, 2010, 07:51:48 PM »
As an extension of the idea - how about encoding in an n-bit integer (0 < n < 32)

Lets say, just as an extension of the concept, that the RGB value was encoded in a 6-bit integer, hence each colour spanned 2-bits (meaning that each colour took 22 values: 0,1,2,3)

Diagrammatically, it might look like this:

Code: [Select]
;;   RGB                |  Red  |Green | Blue |   ;;
;;   ------------------------------------------   ;;
;;   3 x 2 bits         |  2  1 | 2  1 | 2  1 |   ;;
;;   ------------------------------------------   ;;
;;   6-bit integer      | 32 16 | 8  4 | 2  1 |   ;;
;;   ------------------------------------------   ;;
;;                                                ;;
;;   ------------------------------------------   ;;
;;   Example: 45        |  1  0   1  1   0  1 |   ;;
;;   ------------------------------------------   ;;
;;                      |   2   |   3  |   1  |   ;;
;;                      -----------------------   ;;

And, so we might construct a splitting function, using the logic in my post above:

Code: [Select]
(defun 6bit-split ( c )
  (list (lsh (lsh (fix c) 26) -30)
        (lsh (lsh (fix c) 28) -30)
        (lsh (lsh (fix c) 30) -30)
  )
)

Code: [Select]
_$ (6bit-split 45)
(2 3 1)

We could also extend this function to operate on an 'n'-bit integer, by specifying the number of elements encoded [or the bit-width of each element] (as, in the above example we could also have 2 elements, 3-bits wide).

Perhaps such a function may look like this:

Code: [Select]
(defun nbit-split ( c n i / w l ) (setq w (/ n i))
  (repeat i
    (setq l (cons (lsh (lsh (fix c) (- 32 (* w i))) (- w 32)) l)
          i (1- i)
    )
  )
  (reverse l)
)

Where 'c' is the integer to split, 'n' is the bit-width of the integer, and 'i' is the number of elements (must evenly divide 'n').

So, in our 6-bit example above:

Code: [Select]
_$ (nbit-split 45 6 3)
(2 3 1)

And in the earlier example, with our 24-bit RGB value:

Code: [Select]
_$ (nbit-split 13132850 24 3)
(200 100 50)

This is really interesting, Thanks Tim for this thread!  :-)

Interestingly, there is a 6-bit RGB representation!

http://en.wikipedia.org/wiki/List_of_monochrome_and_RGB_palettes#6-bit_RGB
« Last Edit: October 24, 2010, 08:05:20 PM by Lee Mac »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Need help understanding the help
« Reply #20 on: October 25, 2010, 06:07:06 AM »
Wow - I have a lot to learn! - it's going to take me a while to study those :-)

Many thanks Michael, very interesting indeed!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Need help understanding the help
« Reply #21 on: October 25, 2010, 08:26:00 AM »
Very gracious of you to say Lee, any one of you could could pen the same in a similar nerd fest, but thanks -- hope its useful some way. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need help understanding the help
« Reply #22 on: October 25, 2010, 11:18:59 AM »
I haven't had the time to try and understand this, so it's nice to have what you posted Lee.  I think it will help me get a greater grasp on the idea.  Then maybe I'll be able to understand the threads MP linked to.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.